Select platform: [HTML5](https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/live-stream.md.txt "View this page for the HTML5 platform docs.") [Android](https://developers.google.com/interactive-media-ads/docs/sdks/android/client-side/live-stream.md.txt "View this page for the Android platform docs.") [iOS](https://developers.google.com/interactive-media-ads/docs/sdks/ios/client-side/live-stream.md.txt "View this page for the iOS platform docs.") [tvOS](https://developers.google.com/interactive-media-ads/docs/sdks/tvos/client-side/live-stream.md.txt "View this page for the tvOS platform docs.")

<br />

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

> [!IMPORTANT]
> **Key Point:** The [Getting started guide](https://developers.google.com/interactive-media-ads/docs/sdks/android/client-side) and the [Basic Example](https://github.com/googleads/googleads-ima-android/tree/main/basicexample) both implement the [Exoplayer-IMA extension](https://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](https://github.com/googleads/googleads-ima-android/tree/main/AdvancedExample) as a starting point for livestream pre-fetch.

These code snippets show how to add livestream pre-fetch to the
[Advanced Example](https://github.com/googleads/googleads-ima-android/tree/main/AdvancedExample)
, 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 through 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);
}
```