預先擷取預先擷取

IMA SDK 可用來透過直播和隨選影片營利。 如果是直播,您必須針對每個廣告插播發出新的廣告請求。 分散這些要求,確保所有觀眾都沒有提出要求 同時關閉廣告伺服器。

為解決這個問題,IMA SDK 採用 AdsRequest.liveStreamPrefetchSeconds 資源。這個屬性會指定 SDK 的秒數上限 請於呼叫後與廣告伺服器連線 AdsLoader.requestAds()。實際要求時間會隨機增加。適用對象 舉例來說,如果將 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

    ...
  }
}