ライブ配信のプレロール広告を読み込む

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

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