The IMA SDK can be used to monetize live streams as well as video-on-demand. For live streams, you need to make a new ad request for each ad break. Stagger these requests to ensure that all of your viewers aren't requesting ads at the same time and bogging down the ad server(s).
To help with this, the IMA SDK has the AdsRequest.liveStreamPrefetchSeconds
property. This property specifies the maximum number of seconds the SDK
should wait before reaching out to the ad server after you call
AdsLoader.requestAds()
. The actual request time will be randomized. For
example, if you set AdsRequest.liveStreamPrefetchSeconds
to 30, the SDK
waits 0 to 30 seconds after you call AdsLoader.requestAds()
to actually
make the request to the server.
Live stream pre-fetch in practice
We recommend pre-fetching your next ad break as soon as an ad break completes. This ensures the maximum length of time is available for your pre-fetch window. Suppose you have 5 minutes between ad breaks. When an ad break completes, you can request your next ad break with a pre-fetch window of 290 seconds (5 minutes minus 10 seconds, to make sure the requests sent at the end of the pre-fetch window have enough time to resolve):
These code snippets show how to add live stream pre-fetch to the Advanced Example , but the approach can applied to other IMA implementations.
VideoPlayerController.java
/** Ads logic for handling the IMA SDK integration code and events. */ public class VideoPlayerController { // 5 minutes == 300 seconds. Include a 10 second buffer private float AD_INTERVAL = 290; private double AD_TIMEOUT = 300; ... adsManager.addAdEventListener( new AdEvent.AdEventListener() { /** Responds to AdEvents. */ @Override public void onAdEvent(AdEvent adEvent) { ... case ALL_ADS_COMPLETED: if (adsManager != null) { adsManager.destroy(); adsManager = null; } // When pre-fetching for live streams, be sure to destroy the current AdsManager, // in case the tag you requested previously contains post-rolls // (you don't want to play those now). // Pre-fetch the next ad break. // Play those ads in ~5 minutes. In a real-world implementation, // this will likely be done as the result of a message from your // streaming server, not a via the playAdsAfterThisTime parameter // of requestAndPlayAds(). requestAndPlayAds(AD_TIMEOUT); break; default: break; } } ... public void requestAndPlayAds(double playAdsAfterThisTime) { if (currentAdTagUrl == null || currentAdTagUrl == "") { log("No VAST ad tag URL specified"); resumeContent(); return; } // Since you're switching to a new video, tell the SDK the previous video is finished. if (adsManager != null) { adsManager.destroy(); } playButton.setVisibility(View.GONE); // Create the ads request. AdsRequest request = sdkFactory.createAdsRequest(); request.setAdTagUrl(currentAdTagUrl); request.setContentProgressProvider(videoPlayerWithAdPlayback.getContentProgressProvider()); request.setLiveStreamPrefetchSeconds(AD_INTERVAL); playAdsAfterTime = playAdsAfterThisTime; // Request the ad. After the ad is loaded, onAdsManagerLoaded() will be called. adsLoader.requestAds(request); }