IMA SDK を使用して、ライブ ストリームとビデオ オンデマンドの両方を収益化できます。ライブ ストリームの場合は、広告ブレークごとに新しい広告リクエストを行う必要があります。これらのリクエストをずらして、すべての視聴者が同時に広告をリクエストして広告サーバーの処理を遅らせることがないようにします。
この問題を解決するために、IMA SDK には AdsRequest.liveStreamPrefetchSeconds
プロパティがあります。このプロパティは、AdsLoader.requestAds()
を呼び出した後、SDK が広告サーバーに接続するまで待機する最大秒数を指定します。実際のリクエスト時間はランダム化されます。たとえば、AdsRequest.liveStreamPrefetchSeconds
を 30 に設定すると、SDK は AdsLoader.requestAds()
を呼び出してから 0 ~ 30 秒待機してから、実際にサーバーにリクエストを送信します。
ライブ ストリームのプリフェッチの実装
広告ブレークが完了したらすぐに次の広告ブレークをプリフェッチすることをおすすめします。これにより、プリフェッチ ウィンドウで最長の時間が確保されます。広告ブレークの間隔が 5 分の場合を考えてみましょう。広告ブレークが完了したら、290 秒のプリフェッチ ウィンドウで次の広告ブレークをリクエストできます(プリフェッチ ウィンドウの最後に送信されたリクエストが解決されるのに十分な時間を確保するため、5 分から 10 秒を引いた値)。
Objective-C
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {
...
switch (event.type) {
...
case kIMAAdEvent_ALL_ADS_COMPLETED:
IMAAdsRequest *request = [[IMAAdsRequest alloc]
initWithAdTagUrl: self.adTagUrl
adDisplayContainer: self.adDisplayContainer
avPlayerVideoDisplay: self.avPlayerVideoDisplay
pictureInPictureProxy: self.pictureInPictureProxy
userContext: nil];
// set a delay between the end of the last ad
// in the last request, and the first ad from
// the new request
Float64 adGap = 30;
// make sure the request occurs at least five
// seconds before starting the new set of ads
request.liveStreamPrefetchSeconds = adGap - 5;
[self.adsLoader requestAdsWithRequest:request];
// start new ads after adGap seconds have elapsed
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, adGap * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[adsManager start];
});
break;
...
}
...
}
Swift
func adsManager(_ adsManager: IMAAdsManager!, didReceive event: IMAAdEvent!) {
switch event.type {
...
case IMAAdEventType.ALL_ADS_COMPLETED:
let request = IMAAdsRequest(
adTagUrl: AdTagUrl,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: nil)
// set a delay between the end of the last ad
// in the last request, and the first ad from
// the new request
let adGap = 30
// make sure the request occurs at least five
// seconds before starting the new set of ads
request.liveStreamPrefetchSeconds = adGap - 5
adsLoader.requestAds(with: request)
// start new ads after adGap seconds have elapsed
DispatchQueue.main.asyncAfter(deadline: .now() + adGap) {
adsManager.start()
}
break
...
}
}