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 theAdsRequest.liveStreamPrefetchSecondsproperty. This property specifies the maximum number of seconds the SDK
should wait before reaching out to the ad server after you callAdsLoader.requestAds(). The actual request time will be randomized. For
example, if you setAdsRequest.liveStreamPrefetchSecondsto 30, the SDK
waits 0 to 30 seconds after you callAdsLoader.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 livestream pre-fetch to theAdvanced Example, but the approach can applied to other IMA implementations.
VideoPlayerController.java
/**AdslogicforhandlingtheIMASDKintegrationcodeandevents.*/publicclassVideoPlayerController{//5minutes==300seconds.Includea10secondbufferprivatefloatAD_INTERVAL=290;privatedoubleAD_TIMEOUT=300;...adsManager.addAdEventListener(newAdEvent.AdEventListener(){/**RespondstoAdEvents.*/@OverridepublicvoidonAdEvent(AdEventadEvent){...caseALL_ADS_COMPLETED:if(adsManager!=null){adsManager.destroy();adsManager=null;}//Whenpre-fetchingforlivestreams,besuretodestroythecurrentAdsManager,//incasethetagyourequestedpreviouslycontainspost-rolls//(youdon't want to play those now).//Pre-fetchthenextadbreak.//Playthoseadsin~5minutes.Inareal-worldimplementation,//thiswilllikelybedoneastheresultofamessagefromyour//streamingserver,notathroughtheplayAdsAfterThisTimeparameter//ofrequestAndPlayAds().requestAndPlayAds(AD_TIMEOUT);break;default:break;}}...publicvoidrequestAndPlayAds(doubleplayAdsAfterThisTime){if(currentAdTagUrl==null||currentAdTagUrl==""){log("No VAST ad tag URL specified");resumeContent();return;}//Sinceyou're switching to a new video, tell the SDK the previous video is finished.if(adsManager!=null){adsManager.destroy();}playButton.setVisibility(View.GONE);//Createtheadsrequest.AdsRequestrequest=sdkFactory.createAdsRequest();request.setAdTagUrl(currentAdTagUrl);request.setContentProgressProvider(videoPlayerWithAdPlayback.getContentProgressProvider());request.setLiveStreamPrefetchSeconds(AD_INTERVAL);playAdsAfterTime=playAdsAfterThisTime;//Requestthead.Aftertheadisloaded,onAdsManagerLoaded()willbecalled.adsLoader.requestAds(request);}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-05 UTC."],[[["\u003cp\u003eThe IMA SDK supports monetizing live streams and video-on-demand content through ad integration.\u003c/p\u003e\n"],["\u003cp\u003eFor live streams, stagger ad requests to avoid overloading ad servers by utilizing the \u003ccode\u003eAdsRequest.liveStreamPrefetchSeconds\u003c/code\u003e property for pre-fetching.\u003c/p\u003e\n"],["\u003cp\u003eIt's recommended to pre-fetch the next ad break immediately after the current one finishes, maximizing the pre-fetch window for smoother ad transitions.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code snippets demonstrate how to implement live stream pre-fetching within the Advanced Example, showcasing the integration with the IMA SDK.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eAdsRequest.liveStreamPrefetchSeconds\u003c/code\u003e property randomizes the actual request time within the specified maximum duration to further optimize ad delivery.\u003c/p\u003e\n"]]],[],null,["Select platform: [HTML5](/interactive-media-ads/docs/sdks/html5/client-side/live-stream \"View this page for the HTML5 platform docs.\") [Android](/interactive-media-ads/docs/sdks/android/client-side/live-stream \"View this page for the Android platform docs.\") [iOS](/interactive-media-ads/docs/sdks/ios/client-side/live-stream \"View this page for the iOS platform docs.\") [tvOS](/interactive-media-ads/docs/sdks/tvos/client-side/live-stream \"View this page for the tvOS platform docs.\")\n\n\u003cbr /\u003e\n\nThe IMA SDK can be used to monetize live streams as well as video-on-demand.\nFor live streams, you need to make a new ad request for each ad break.\nStagger these requests to ensure that all of your viewers aren't requesting\nads at the same time and bogging down the ad server(s).\n\nTo help with this, the IMA SDK has the `AdsRequest.liveStreamPrefetchSeconds`\nproperty. This property specifies the maximum number of seconds the SDK\nshould wait before reaching out to the ad server after you call\n`AdsLoader.requestAds()`. The actual request time will be randomized. For\nexample, if you set `AdsRequest.liveStreamPrefetchSeconds` to 30, the SDK\nwaits 0 to 30 seconds after you call `AdsLoader.requestAds()` to actually\nmake the request to the server.\n\nLive stream pre-fetch in practice\n\nWe recommend pre-fetching your next ad break as soon as an ad break completes.\nThis ensures the maximum length of time is available for your pre-fetch window.\nSuppose you have 5 minutes between ad breaks. When an ad break completes,\nyou can request your next ad break with a pre-fetch window of 290 seconds\n(5 minutes minus 10 seconds, to make sure the requests sent at the end of\nthe pre-fetch window have enough time to resolve):\n| **Key Point:** The [Getting started guide](/interactive-media-ads/docs/sdks/android/client-side) and the [Basic Example](//github.com/googleads/googleads-ima-android/tree/main/basicexample) both implement the [Exoplayer-IMA extension](//github.com/google/ExoPlayer/tree/release-v2/extensions/ima) which handles the setup of the AdsManager. Because of this, these resources are not able to be used as a starting point for this guide. We recommend using the [Advanced Example](//github.com/googleads/googleads-ima-android/tree/main/AdvancedExample) as a starting point for livestream pre-fetch.\n\nThese code snippets show how to add livestream pre-fetch to the\n[Advanced Example](//github.com/googleads/googleads-ima-android/tree/main/AdvancedExample)\n, but the approach can applied to other IMA implementations.\n\n**VideoPlayerController.java** \n\n```gdscript\n/** Ads logic for handling the IMA SDK integration code and events. */\npublic class VideoPlayerController {\n\n // 5 minutes == 300 seconds. Include a 10 second buffer\n private float AD_INTERVAL = 290;\n private double AD_TIMEOUT = 300;\n\n...\n\n adsManager.addAdEventListener(\n new AdEvent.AdEventListener() {\n /** Responds to AdEvents. */\n @Override\n public void onAdEvent(AdEvent adEvent) {\n\n ...\n\n case ALL_ADS_COMPLETED:\n if (adsManager != null) {\n adsManager.destroy();\n adsManager = null;\n }\n\n // When pre-fetching for live streams, be sure to destroy the current AdsManager,\n // in case the tag you requested previously contains post-rolls\n // (you don't want to play those now).\n\n // Pre-fetch the next ad break.\n // Play those ads in ~5 minutes. In a real-world implementation,\n // this will likely be done as the result of a message from your\n // streaming server, not a through the playAdsAfterThisTime parameter\n // of requestAndPlayAds().\n requestAndPlayAds(AD_TIMEOUT);\n break;\n default:\n break;\n }\n }\n\n...\n\npublic void requestAndPlayAds(double playAdsAfterThisTime) {\n if (currentAdTagUrl == null || currentAdTagUrl == \"\") {\n log(\"No VAST ad tag URL specified\");\n resumeContent();\n return;\n }\n\n // Since you're switching to a new video, tell the SDK the previous video is finished.\n if (adsManager != null) {\n adsManager.destroy();\n }\n\n playButton.setVisibility(View.GONE);\n\n // Create the ads request.\n AdsRequest request = sdkFactory.createAdsRequest();\n request.setAdTagUrl(currentAdTagUrl);\n request.setContentProgressProvider(videoPlayerWithAdPlayback.getContentProgressProvider());\n request.setLiveStreamPrefetchSeconds(AD_INTERVAL);\n\n playAdsAfterTime = playAdsAfterThisTime;\n\n // Request the ad. After the ad is loaded, onAdsManagerLoaded() will be called.\n adsLoader.requestAds(request);\n}\n```"]]