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