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
...
}
}