ストリームの広告ブレークを事前読み込みする

プラットフォームを選択: HTML5 Android iOS tvOS

IMA SDK は、ライブ ストリームだけでなく、オンデマンド動画の収益化にも使用できます。 ライブ ストリームの場合は、ミッドロール挿入点ごとに新しい広告リクエストを行う必要があります。すべての視聴者が同時に広告をリクエストして広告サーバーに負荷がかからないように、これらのリクエストを分散してください。

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

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

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

// 5 minutes == 300 seconds. Include a 10 second buffer
var AD_INTERVAL = 290;

function onAdEvent(adEvent) {
  var ad = adEvent.getAd();
  switch(adEvent.type) {
    case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
      // Pre-fetch our next ad break.
      requestAds();
      // Play those ads in 5 minutes. In a real-world implementation,
      // this is likely done as the result of a message from your
      // streaming server, not a timeout.
      setTimeout(playAds, AD_INTERVAL * 1000);// Convert to ms.
  }
}

function requestAds() {
  // Destroy the current AdsManager, in case the tag you requested previously
  // contains post-rolls (don't play those now).
  if (adsManager) {
    adsManager.destroy();
  }
  // Your AdsLoader will be set up on page-load. You should re-use the same
  // AdsLoader for every request. For more info on setting up the AdsLoader,
  // see the IMA HTML5 client-side "Set up the IMA SDK" guide:
  // https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/get-started
  if (adsLoader) {
    // Reset the IMA SDK.
    adsLoader.contentComplete();
  }
  var adsRequest = new google.ima.AdsRequest();
  adsRequest.adTagUrl = '...';
  adsRequest.linearAdSlotWidth = <linear_width>;
  adsRequest.linearAdSlotHeight = <linear_height>;
  adsRequest.nonLinearAdSlotWidth = <nonlinear_width>;
  adsRequest.nonLinearAdSlotHeight = <nonlinear_height>;
  adsRequest.liveStreamPrefechSeconds = AD_INTERVAL;
  adsLoader.requestAds(adsRequest);
}

function playAds() {
  adsManager.init(<linear_width>,  <linear_height>);
  adsManager.start();
}