โหลดโฆษณาตอนต้นสำหรับสตรีมแบบสด

SDK โฆษณาสื่ออินเทอร์แอกทีฟ (IMA) ของ Google สำหรับ iOS

คุณใช้ IMA SDK เพื่อสร้างรายได้จากไลฟ์สดและวิดีโอออนดีมานด์ได้ สำหรับไลฟ์สด คุณต้องส่งคำขอโฆษณาใหม่สำหรับช่วงพักโฆษณาแต่ละช่วง คุณควรส่งคำขอเหล่านี้แบบเหลื่อมกันเพื่อให้ผู้ชมทั้งหมดไม่ส่งคำขอ โฆษณาพร้อมกันและทำให้เซิร์ฟเวอร์โฆษณาทำงานช้าลง

IMA SDK มีAdsRequest.liveStreamPrefetchSeconds พร็อพเพอร์ตี้เพื่อช่วยในเรื่องนี้ พร็อพเพอร์ตี้นี้ระบุจำนวนวินาทีสูงสุดที่ SDK ควรรอก่อนที่จะติดต่อเซิร์ฟเวอร์โฆษณาหลังจากที่คุณเรียกใช้ AdsLoader.requestAds() ระบบจะสุ่มเวลาคำขอจริง ตัวอย่างเช่น หากตั้งค่า AdsRequest.liveStreamPrefetchSeconds เป็น 30 SDK จะรอ 0-30 วินาทีหลังจากที่คุณเรียกใช้ AdsLoader.requestAds() เพื่อส่งคำขอไปยังเซิร์ฟเวอร์จริงๆ

การดึงข้อมูลล่วงหน้าของไลฟ์สดในทางปฏิบัติ

เราขอแนะนำให้ดึงข้อมูลช่วงพักโฆษณาถัดไปล่วงหน้าทันทีที่ช่วงพักโฆษณาสิ้นสุดลง ซึ่งจะช่วยให้มีระยะเวลาสูงสุดสำหรับหน้าต่างการดึงข้อมูลล่วงหน้า สมมติว่าคุณมีเวลา 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

    ...
  }
}