ライブ配信のプリフェッチ

IMA SDK を使用して、ライブ ストリームやビデオ オンデマンドを収益化できます。 ライブ配信の場合は、ミッドロール挿入点ごとに新しい広告リクエストを行う必要があります。 これらのリクエストを適宜調整して、すべての視聴者が同時に広告をリクエストして広告サーバーで障害が発生する事態を回避します。

そのため、IMA SDK には AdsRequest.liveStreamPrefetchSeconds プロパティが用意されています。このプロパティは、AdsLoader.requestAds() を呼び出した後に SDK が広告サーバーに到達するまでの最大待機時間(秒)を指定します。実際のリクエスト時間はランダム化されます。たとえば、AdsRequest.liveStreamPrefetchSeconds を 30 に設定した場合、SDK は、AdsLoader.requestAds() を呼び出してから実際にサーバーにリクエストを送信してから 0 ~ 30 秒待機します。

ライブ ストリームのプリフェッチの実践

広告ブレークが完了したら、すぐに次の広告ブレークをプリフェッチすることをおすすめします。これにより、プリフェッチ ウィンドウで使用可能な最大時間を確保できます。ミッドロール挿入点の間隔が 5 分であるとします。広告ブレークが完了したら、290 秒のプリフェッチ枠(5 分から 10 秒を引いた時間)で次の広告ブレークをリクエストできます。これにより、プリフェッチ ウィンドウの終了時に送信されたリクエストを解決するのに十分な時間を確保できます。

以下のコード スニペットは、高度な例にライブ ストリームのプリフェッチを追加する方法を示していますが、他の IMA の実装にもこのアプローチを適用できます。

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);
}