我不确定您是否将 IMA 扩展与 ImaAdsLoader 一起使用,还是您的 AdsLoader 的自定义实现(它是核心库的一部分)(不需要 IMA 扩展):
使用 IMA 广告代码
如果您使用 IMA 扩展,您需要创建一个ImaAdsLoader
带有来自 IMA/DoubleClick 的广告代码 (样本标签):
new ImaAdsLoader(this, adTagUri);
该标签指向一个 VMAP/VAST 文件,该文件定义了广告的播放时间。因此,使用此解决方案,您无法以编程方式告诉玩家广告的位置。相反,所有这些信息都是从广告代码加载的。
自定义 AdsLoader 实施
您可以创建自己的实现AdsLoader
您可以将其传递给构造函数或 AdsMediaSource 而不是 IMA 版本。这AdsMediaSource
然后会调用attachPlayer(player, eventListener, adUiViewGroup)
您的广告加载器的方法。第二个参数是AdsLoader.EventListener
与其中AdsMediaSource
可以收到有关广告加载器状态的通知。
如果此时您已经知道广告的信息以及在什么位置播放广告,则只需在连接播放器时调用一次监听器即可:
// create playback state with two ad groups (at 0 and 30 seconds)
AdPlaybackState adPlaybackState = new AdPlaybackState(0, 30 * C.MICROS_PER_SECOND);
// add the uri of the first ad of the first ad group
adPlaybackState = adPlaybackState.withAdUri(0, 0, mp4UriWithAd);
// add the uri of the first ad of the second ad group
adPlaybackState = adPlaybackState.withAdUri(1, 0, mp4UriWithAd);
// add the uri of the second ad of the second ad group
adPlaybackState = adPlaybackState.withAdUri(1, 1, mp4UriWithAd);
// pass to AdsLoader.EventListener
eventListener.onAdPlaybackState(adPlaybackState);
但是,您可能需要先从网络加载广告信息。在这种情况下,您只需在检索到有关广告的新信息时更新播放状态即可。只需致电onAdPlaybackState
再次更新。