You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关闭InterstitialAd插页广告后音频未停止,求解决办法

关闭插页广告后InterstitialAd视频声音仍播放的解决办法

问题描述

关闭InterstitialAd插页广告后,广告视频的声音并未停止,且尝试在InterstitialAd.load()中寻找onAdClosed方法未果。

你的代码片段

广告加载代码

InterstitialAd.load(
  adUnitId: adUnitId,
  request: const AdRequest(),
  adLoadCallback: InterstitialAdLoadCallback(
    onAdLoaded: (Ad ad) {
      interstitialAd = ad as InterstitialAd?;
      if (showAfterLoad) showInterstitialAd();
      interstitialAd!.dispose();
    },
    onAdFailedToLoad: (LoadAdError error) {
      print('Failed to load ad: $error');

      interstitialAd = null; // Eğer reklam yüklenemezse null yap
    },
  ),
);
}

Future<void> showInterstitialAd() async {
  if (interstitialAd != null) {
    return Future(() => interstitialAd!.show());
  }
}

广告调用代码

final FirebaseService firebaseService = FirebaseService();
FavoriteServices favoriteServices = Get.put(FavoriteServices());
final GoogleAds googleAds = GoogleAds();
Timer? _refreshTimer;
Map<String, List<News>> newsDataMap = {
  'sonDakika': [],
  'dunya': [],
  'spor': [],
  'magazin': [],
};

@override
void initState() {
  super.initState();
  //firebaseService.startTimer(); // Tüm kategoriler için timer'ı başlat.
  googleAds.loadBannerAd(adLoaded: () {
    setState(() {});
  });
  googleAds.loadInterstitalAd();
  _startRefreshTimer(); // Yenileme timer'ını başlat.
}

解决办法

1. 修正提前销毁广告的错误

你在onAdLoaded回调中直接调用interstitialAd!.dispose(),这会导致广告还没完成展示、关闭的完整流程就被销毁,音频资源无法正常释放。必须等到广告完全关闭后再执行销毁操作。

2. 通过FullScreenContentCallback监听广告关闭事件

InterstitialAd的关闭回调不在InterstitialAdLoadCallback中,需要通过FullScreenContentCallback来监听广告的生命周期事件,包括关闭。修改后的加载代码如下:

InterstitialAd.load(
  adUnitId: adUnitId,
  request: const AdRequest(),
  adLoadCallback: InterstitialAdLoadCallback(
    onAdLoaded: (Ad ad) {
      interstitialAd = ad as InterstitialAd?;
      // 配置全屏广告的内容回调
      interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
        // 广告关闭时触发
        onAdDismissedFullScreenContent: (InterstitialAd ad) {
          // 销毁广告资源,释放音频
          ad.dispose();
          interstitialAd = null;
        },
        // 广告展示失败时触发,同样要销毁资源
        onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
          print('Ad failed to show: $error');
          ad.dispose();
          interstitialAd = null;
        },
        // 可选:监听广告展示成功事件
        onAdShowedFullScreenContent: (InterstitialAd ad) {
          print('Ad showed successfully');
        },
      );
      
      if (showAfterLoad) showInterstitialAd();
    },
    onAdFailedToLoad: (LoadAdError error) {
      print('Failed to load ad: $error');
      interstitialAd = null;
    },
  ),
);

3. 确保广告展示方法的有效性

你的showInterstitialAd方法可以保留,但要保证在调用show()时,广告实例是未被销毁的有效状态。

关键注意点

  • 广告资源的销毁必须在广告关闭或展示失败后执行,不能提前调用dispose()。
  • FullScreenContentCallback能覆盖广告全屏展示阶段的所有关键事件,是管理广告生命周期的核心回调。

内容的提问来源于stack exchange,提问作者Farid Ahmadov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 16:42:27