setAdWillAutoPlay(autoPlay)

842 views
Skip to first unread message

Eric GoalsMedia

unread,
Mar 21, 2016, 9:33:26 AM3/21/16
to Interactive Media Ads SDK
Hello,

I hope to play automatically.

“setAdWillAutoPlay(autoPlay)” I do not know how to set this parameter.


This is my demo. Now not automatically play. Please help me to achieve automatic playback.

Thanks

Eric GoalsMedia

unread,
Mar 21, 2016, 10:25:35 AM3/21/16
to Interactive Media Ads SDK
// Copyright 2013 Google Inc. All Rights Reserved.
// You may study, modify, and use this example for any purpose.
// Note that this example is provided "as is", WITHOUT WARRANTY
// of any kind either expressed or implied.

var adsManager;
var adsLoader;
var adDisplayContainer;
var intervalTimer;
var playButton;
var videoContent;

function init() {
  videoContent = document.getElementById('contentElement');
  playButton = document.getElementById('playButton');
  playButton.addEventListener('click', playAds);
  setUpIMA();
}

function setUpIMA() {
  // Create the ad display container.
  createAdDisplayContainer();
  // Create ads loader.
  adsLoader = new google.ima.AdsLoader(adDisplayContainer);
  // Listen and respond to ads loaded and error events.
  adsLoader.addEventListener(
      google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
      onAdsManagerLoaded,
      false);
  adsLoader.addEventListener(
      google.ima.AdErrorEvent.Type.AD_ERROR,
      onAdError,
      false);

  // Request video ads.
  var adsRequest = new google.ima.AdsRequest();

  // Specify the linear and nonlinear slot sizes. This helps the SDK to
  // select the correct creative if multiple are returned.
  adsRequest.linearAdSlotWidth = 640;
  adsRequest.linearAdSlotHeight = 400;

  adsRequest.nonLinearAdSlotWidth = 640;
  adsRequest.nonLinearAdSlotHeight = 150;

  adsLoader.requestAds(adsRequest);
}


function createAdDisplayContainer() {
  // We assume the adContainer is the DOM id of the element that will house
  // the ads.
  adDisplayContainer = new google.ima.AdDisplayContainer(
      document.getElementById('adContainer'), videoContent);
}

function playAds() {
  // Initialize the container. Must be done via a user action on mobile devices.
  videoContent.load();
  adDisplayContainer.initialize();

  try {
    // Initialize the ads manager. Ad rules playlist will start at this time.
    adsManager.init(640, 360, google.ima.ViewMode.NORMAL);
    // Call play to start showing the ad. Single video and overlay ads will
    // start at this time; the call will be ignored for ad rules.
    adsManager.start();
  } catch (adError) {
    // An error may be thrown if there was a problem with the VAST response.
    videoContent.play();
  }
}

function onAdsManagerLoaded(adsManagerLoadedEvent) {
  // Get the ads manager.
  var adsRenderingSettings = new google.ima.AdsRenderingSettings();
  adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;
  // videoContent should be set to the content video element.
  adsManager = adsManagerLoadedEvent.getAdsManager(
      videoContent, adsRenderingSettings);

  // Add listeners to the required events.
  adsManager.addEventListener(
      google.ima.AdErrorEvent.Type.AD_ERROR,
      onAdError);
  adsManager.addEventListener(
      google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,
      onContentPauseRequested);
  adsManager.addEventListener(
      google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,
      onContentResumeRequested);
  adsManager.addEventListener(
      google.ima.AdEvent.Type.ALL_ADS_COMPLETED,
      onAdEvent);

  // Listen to any additional events, if necessary.
  adsManager.addEventListener(
      google.ima.AdEvent.Type.LOADED,
      onAdEvent);
  adsManager.addEventListener(
      google.ima.AdEvent.Type.STARTED,
      onAdEvent);
  adsManager.addEventListener(
      google.ima.AdEvent.Type.COMPLETE,
      onAdEvent);
}

function onAdEvent(adEvent) {
  // Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED)
  // don't have ad object associated.
  var ad = adEvent.getAd();
  switch (adEvent.type) {
    case google.ima.AdEvent.Type.LOADED:
      // This is the first event sent for an ad - it is possible to
      // determine whether the ad is a video ad or an overlay.
      if (!ad.isLinear()) {
        // Position AdDisplayContainer correctly for overlay.
        // Use ad.width and ad.height.
        videoContent.play();
      }
      break;
    case google.ima.AdEvent.Type.STARTED:
      // This event indicates the ad has started - the video player
      // can adjust the UI, for example display a pause button and
      // remaining time.
      if (ad.isLinear()) {
        // For a linear ad, a timer can be started to poll for
        // the remaining time.
        intervalTimer = setInterval(
            function() {
              var remainingTime = adsManager.getRemainingTime();
            },
            300); // every 300ms
      }
      break;
    case google.ima.AdEvent.Type.COMPLETE:
      // This event indicates the ad has finished - the video player
      // can perform appropriate UI actions, such as removing the timer for
      // remaining time detection.
      if (ad.isLinear()) {
        clearInterval(intervalTimer);
      }
      break;
  }
}

function onAdError(adErrorEvent) {
  // Handle the error logging.
  console.log(adErrorEvent.getError());
  adsManager.destroy();
}

function onContentPauseRequested() {
  videoContent.pause();
  // This function is where you should setup UI for showing ads (e.g.
  // display ad timer countdown, disable seeking etc.)
  // setupUIForAds();
}

function onContentResumeRequested() {
  videoContent.play();
  // This function is where you should ensure that your UI is ready
  // to play content. It is the responsibility of the Publisher to
  // implement this function when necessary.
  // setupUIForContent();

}

// Wire UI element references and UI event listeners.
init();

Vu Chau (IMA SDK Team)

unread,
Mar 21, 2016, 2:28:31 PM3/21/16
to Interactive Media Ads SDK
Hi Eric,

setAdWillAutoPlay() will have no effect on ad playback, as noted in the doc.  It is only to serve a compliance purpose whereby it will let the ad server know that you intend for the implementation to auto-play the ad.

As to actually auto-play the ad, there was a confusion about the workflow in our sample code.  So, we will update that ASAP.

In the meantime, if you want to see the effect of autoplay for single ad (doing this will affect VMAP and ad rules, so we recommend waiting for the above fix to go live), you can temporarily move your AdsManager.init() and AdsManager.start() logic (i.e. the try-catch block in the sample code) to the onAdsManagerLoaded() handler.  Then, programmatically call playAds() after sending out an ad request.  Doing that will cause autoplay to take effect on page load.

Vu Chau
IMA SDK Team

Eric GoalsMedia

unread,
Apr 7, 2016, 9:51:31 AM4/7/16
to Interactive Media Ads SDK
Hello,

IMA SDK  Autoplay has been resolved?

Can you provide autoplay demo yet?

Thanks

Vu Chau (IMA SDK Team)

unread,
Apr 7, 2016, 4:35:29 PM4/7/16
to Interactive Media Ads SDK
Hi Eric,

If you implement the logic outlined in my previous response, that should handle autoplaying for now.

Here's a live implementation should you need it.

Vu Chau
IMA SDK Team

Gor Hovhannisyan

unread,
May 22, 2017, 3:16:38 PM5/22/17
to Interactive Media Ads SDK

please can i see this snippet?

Chris Feldman (IMA SDK Team)

unread,
May 22, 2017, 3:59:23 PM5/22/17
to Interactive Media Ads SDK
Hi Gor,

What snippet do you need? The sample that Vu posted is still live. You can view the source code on GitHub.

If you have a more specific issue, please open a thread with more detailed information and I will be happy to assist you.

Regards,
Chris Feldman
IMA SDK Team
Reply all
Reply to author
Forward
0 new messages