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