可以在Flutter项目中使用flutter_admob库来加载Admob广告,并使用视频播放器插件来显示视频。以下是示例代码:
'''dart import 'package:flutter/material.dart'; import 'package:video_player/video_player.dart'; import 'package:flutter_admob/flutter_admob.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); }
class _MyAppState extends State
AdmobInterstitial interstitialAd = AdmobInterstitial(
adUnitId: 'YOUR_AD_UNIT_ID',
listener: (AdmobAdEvent event, Map
@override void initState() { controller = VideoPlayerController.asset('assets/video.mp4') ..initialize().then(() { setState(() {}); // video initialized so need to rebuild widget to play the video }); super.initState(); interstitialAd.load(); }
@override void dispose() { _controller.dispose(); interstitialAd.dispose(); super.dispose(); }
@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Admob Advertisement Video Example'), ), body: _showAd ? AdmobBanner( adUnitId: 'YOUR_AD_UNIT_ID', adSize: AdmobBannerSize.BANNER, ) : GestureDetector( onTap: () { setState(() { _controller.value.isPlaying ? _controller.pause() : _controller.play(); }); }, child: Center( child: _controller.value.initialized ? AspectRatio( aspectRatio: _controller.value.aspectRatio, child: VideoPlayer(_controller), ) : Container(), ), ), floatingActionButton: FloatingActionButton( onPressed: () { if (interstitialAd.isLoaded) { interstitialAd.show(); } else { getVideo(); } }, child: Icon(Icons.play_arrow), ), ),
上一篇:admob广告加载失败:3。