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