I have a screen with a list youtube links. When i click one of them i am being taken to the youtube player screen when i want to go back, i have placed onWillPop to display an admob interstetial Ad when user is navigating back. Does this violate youtube policies? Or its okay to Place an ad before or after a youtube video screen? This is my implemention.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:youtube_player_iframe/youtube_player_iframe.dart';
class YoutubePlayerScreen extends StatefulWidget {
final String youtubeUrl;
final String name;
final String id;
const YoutubePlayerScreen(
{required this.youtubeUrl, required this.name, required this.id});
@override
_YoutubePlayerScreenState createState() => _YoutubePlayerScreenState();
}
class _YoutubePlayerScreenState extends State<YoutubePlayerScreen> {
late YoutubePlayerController _controller;
InterstitialAd? _interstitialAd;
int _interstitialLoadAttempts = 0;
bool _isFullScreen = false; // Track full-screen state
void _initAd() {
InterstitialAd.load(
adUnitId: 'ca-app-pub-xxxxxxxxxx/xxxxxxxxx',
request: const AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
// ad loaded
onAdLoaded: (InterstitialAd ad) {
_interstitialAd = ad;
_interstitialLoadAttempts = 0;
},
// ad failed to load
onAdFailedToLoad: (e) {
_interstitialLoadAttempts += 1;
_interstitialAd = null;
if (_interstitialLoadAttempts >= 3) {
_initAd();
}
},
),
);
}
@override
void dispose() {
if (!_isFullScreen) {
showInterstitialAd();
}
_interstitialAd?.dispose();
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
overlays: SystemUiOverlay.values);
super.dispose();
}
void showInterstitialAd() {
if (_interstitialAd != null && !_isFullScreen) {
// Show ad only if not in full-screen mode
_interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdDismissedFullScreenContent: (InterstitialAd ad) {
ad.dispose();
_initAd();
},
onAdFailedToShowFullScreenContent: (ad, e) {
ad.dispose();
_initAd();
},
);
_interstitialAd!.show();
}
}
Future<bool> _onWillPop() async {
if (_isFullScreen) {
_isFullScreen = false;
return true; // Allow pop when in full-screen mode
} else {
showInterstitialAd();
return true; // Allow pop when not in full-screen mode
}
}
@override
void initState() {
super.initState();
_initAd();
_controller = YoutubePlayerController(
initialVideoId: widget.id,
params: YoutubePlayerParams(
showControls: false,
mute: false,
showFullscreenButton: true,
autoPlay: true,
loop: false,
annotations: false, // Hide video annotations
),
);
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: YoutubePlayerIFrame(
controller: _controller,
aspectRatio: 16 / 9,
),
);
}
}