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