IMA SDK は、ライブ ストリームだけでなく、オンデマンド動画の収益化にも使用できます。 ライブストリームの場合、広告ブレークごとに新しい広告リクエストを行う必要があります。 すべての視聴者が同時に広告をリクエストして広告サーバーに負荷がかからないように、これらのリクエストを分散させます。
このために、IMA SDK には AdsRequest.liveStreamPrefetchSeconds
プロパティがあります。このプロパティは、SDK
が広告サーバーに接続するまで待機する最大秒数を指定します。
AdsLoader.requestAds()実際のリクエスト時間はランダム化されます。たとえば、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); }