Adsense for games IMA SDK HTML5 Playback Error

640 Views Asked by At

Error: Failed to initialize ad playback element before starting ad playback.

We can not get an ad to show for us. It used to work just fine, and then all of a sudden this error is now being thrown. We haven't changed the code since it worked for years.

We use this on gaming websites, we show a Video ad, and then use onMediaEnd() to show the game after the video completes or encounters an error.

<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>

Using this:

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

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

1

There are 1 best solutions below

0
On

I encountered exactly the same thing with my online games (desktop). Surprisingly I could get the video ads running again after adding

adDisplayContainer.initialize();

although the docs are saying that 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();