这个问题可能是因为在使用 Android IMA SDK 预加载广告时未正确初始化播放器或未设置广告播放器的监听器。以下是一个示例代码,说明如何正确初始化并获得广告事件:
// 创建一个 ImaAdsLoader 实例
imaAdsLoader = ImaAdsLoader.Builder(activity)
.setAdEventListener(adEventListener)
.build()
// 创建一个 ImaAdsLoader.OnAdErrorListener 监听器
private val adEventListener = object : ImaAdsLoader.OnAdEventListener {
override fun onAdEvent(adEvent: AdEvent) {
// 处理广告事件
when (adEvent.type) {
AdEventType.LOADED -> {
...
}
AdEventType.STARTED -> {
...
}
AdEventType.PAUSED -> {
...
}
AdEventType.RESUMED -> {
...
}
AdEventType.COMPLETED -> {
...
}
AdEventType.CONTENT_PAUSE_REQUESTED -> {
...
}
AdEventType.CONTENT_RESUME_REQUESTED -> {
...
}
AdEventType.FIRST_QUARTILE -> {
...
}
AdEventType.SECOND_QUARTILE -> {
...
}
AdEventType.THIRD_QUARTILE -> {
...
}
AdEventType.IMPRESSION -> {
...
}
AdEventType.CLICKED -> {
...
}
AdEventType.ICON_TAPPED -> {
...
}
AdEventType.ALL_ADS_COMPLETED -> {
...
}
}
}
override fun onAdError(error: AdError) {
// 处理广告错误
...
}
}
// 将 AdsLoader 传递给 a CastVideoPlayer 对象
private fun setupPlayback() {
...
castVideoPlayer.setAdsLoader(imaAdsLoader)
...
}