我正在实施chewie视频播放器,并在播放器的后退按钮上投放广告。通过将Material App小部件实现为MyApp类,视频播放器可以正常运行,但是广告消失了;另一方面,当我从MyApp屏幕上删除Material App时,广告可以正常运行,但是视频播放器不放置在后退按钮上。
//import 'package:chewie_tutorial/chewie_list_item.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'chewie_list_item.dart';
import 'package:firebase_admob/firebase_admob.dart';
import 'package:video_player/video_player.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Orientatoin();
return MaterialApp(
home: MYHomePage(),
);
}
}
int counter=0;
class MYHomePage extends StatelessWidget {
static final MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
keywords: ['Games', 'Puzzles'],
);
BannerAd bannerAd;
InterstitialAd interstitialAd;
RewardedVideoAd rewardedVideoAd;
BannerAd buildBanner() {
return BannerAd(
adUnitId: BannerAd.testAdUnitId,
size: AdSize.banner,
listener: (MobileAdEvent event) {
print(event);
});
}
InterstitialAd buildInterstitial() {
return InterstitialAd(
adUnitId: InterstitialAd.testAdUnitId,
targetingInfo: targetingInfo,
listener: (MobileAdEvent event) {
if (event == MobileAdEvent.failedToLoad) {
interstitialAd..load();
} else if (event == MobileAdEvent.closed) {
interstitialAd = buildInterstitial()..load();
}
print(event);
});
}
@override
Widget build(BuildContext context) {
FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
bannerAd = buildBanner()..load();
interstitialAd = buildInterstitial()..load();
Future<bool> _onBackPressed() {
if(counter<1){
interstitialAd
..load()
..show();
counter++;
}
else{
bannerAd.dispose();
Navigator.pop(context, true);
}
}
return WillPopScope(
child:Scaffold(
body: ListView(
children: <Widget>[
ChewieListItem(
videoPlayerController: VideoPlayerController.network(
'https://bk7l2g37dx53-hls-live.5centscdn.com/adom/5b97b7d64a64c7cd54c18b07e93570a1.sdp/index.m3u8',
),
),
],
),
),
onWillPop: _onBackPressed,
);
}
}
void Orientatoin() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
}