Adsense 游戏 IMA SDK HTML5 播放错误

Posted

技术标签:

【中文标题】Adsense 游戏 IMA SDK HTML5 播放错误【英文标题】:Adsense for games IMA SDK HTML5 Playback Error 【发布时间】:2018-10-02 20:09:50 【问题描述】:

错误:无法在开始播放广告之前初始化广告播放元素。

我们无法为我们展示广告。它曾经工作得很好,然后突然间这个错误现在被抛出了。自从它工作多年以来,我们没有更改过代码。

我们在游戏网站上使用它,展示视频广告,然后在视频播放完毕或遇到错误后使用onMediaEnd() 展示游戏。

<script>

var adVideoContainer   = document.getElementById('ad-video');
var adDisplayContainer = new google.ima.AdDisplayContainer(adVideoContainer);
var adsLoader          = new google.ima.AdsLoader(adDisplayContainer);

adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, function(adEvent)
    console.log('ADS_MANAGER_LOADED');
    onAdsManagerLoaded(adEvent);
, true);

adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, function(adEvent) 
    console.log('AD_ERROR');
    onMediaEnd();
,false);

function onAdsManagerLoaded(adEvent)

    var adsRenderingSettings = new google.ima.AdsRenderingSettings();

    var adsManager = adEvent.getAdsManager(
        currentTime: 0
    );

    window.adsManager = adsManager;

    adsManager.addEventListener( google.ima.AdErrorEvent.Type.AD_ERROR, function(adEvent) 
        console.log('AD_ERROR');
        onMediaEnd();
    );

    adsManager.addEventListener( google.ima.AdEvent.Type.ALL_ADS_COMPLETED, function(adEvent) 
        console.log('ALL_ADS_COMPLETED');
        onMediaEnd();
    );

    adsManager.addEventListener( google.ima.AdEvent.Type.COMPLETE, function(adEvent) 
        console.log('COMPLETE');
        onMediaEnd();
    );

    adsManager.addEventListener(google.ima.AdEvent.Type.LOADED,function(adEvent)
        console.log('LOADED');

        var ad = adEvent.getAd();
        var adData = adEvent.getAdData();

        console.log(adData);

        if (!ad.isLinear())
        
            // show game content
            onMediaEnd();
        
        else
        
            // Ad is being shown, lets do nothing for now...
        
    );


    try
    
        adsManager.init(640, 480, google.ima.ViewMode.NORMAL);
        adsManager.start();
    
    catch (adError)
    
        // this is currently where the ad keeps failing! 

        console.log(adError);
        onMediaEnd();
    



    var adsRequest = new google.ima.AdsRequest();
    adsRequest.adTagUrl               = '<?=$adsenseVASTURL?>';
    adsRequest.linearAdSlotWidth      = 640;
    adsRequest.linearAdSlotHeight     = 480;
    adsRequest.nonLinearAdSlotHeight  = 480;
    adsRequest.nonLinearAdSlotWidth   = 640;
    adsRequest.forceNonLinearFullSlot = true;

    /*adsRequest.setAdWillAutoPlay(false);
    adsRequest.setAdWillPlayMuted(true);*/

    adsLoader.requestAds(adsRequest);

</script>

使用这个:

https://github.com/googleads/googleads-ima-html5

https://developers.google.com/interactive-media-ads/docs/sdks/html5/v3/apis#ima.AdsRequest.setAdWillPlayMuted

【问题讨论】:

【参考方案1】:

我的在线游戏(桌面)遇到了完全相同的情况。 令人惊讶的是,添加后我可以让视频广告再次运行

adDisplayContainer.initialize();

虽然文档说this method has no effect on desktop platforms...

var adVideoContainer   = document.getElementById('ad-video');
var adDisplayContainer = new google.ima.AdDisplayContainer(adVideoContainer);
var adsLoader          = new google.ima.AdsLoader(adDisplayContainer);
adDisplayContainer.initialize();

【讨论】:

以上是关于Adsense 游戏 IMA SDK HTML5 播放错误的主要内容,如果未能解决你的问题,请参考以下文章

如何手动触发 HTML5 IMA SDK 点击事件?

Google IMA HTML5 SDK:获取/设置广告时长

IMA SDK HTML5 视频播放器控件未显示

HTML5:Google IMA 显示默认进度条和倒计时

如何删除google-ima SDK中的广告倒计时器?

Android IMA SDK“了解更多”自定义点击监听