Tải quảng cáo trước video cho sự kiện phát trực tiếp

SDK Quảng cáo tương tác trên phương tiện truyền thông (IMA) của Google dành cho iOS.

Bạn có thể sử dụng IMA SDK để kiếm tiền từ luồng phát trực tiếp cũng như video theo yêu cầu. Đối với luồng phát trực tiếp, bạn cần đưa ra yêu cầu quảng cáo mới cho mỗi điểm chèn quảng cáo. Hãy phân bổ thời gian cho các yêu cầu này để đảm bảo rằng tất cả người xem không yêu cầu quảng cáo cùng một lúc và làm chậm(các) máy chủ quảng cáo.

Để hỗ trợ việc này, IMA SDK có thuộc tính AdsRequest.liveStreamPrefetchSeconds. Thuộc tính này chỉ định số giây tối đa mà SDK sẽ chờ trước khi liên hệ với máy chủ quảng cáo sau khi bạn gọi AdsLoader.requestAds(). Thời gian yêu cầu thực tế sẽ được chọn ngẫu nhiên. Ví dụ: nếu bạn đặt AdsRequest.liveStreamPrefetchSeconds thành 30, thì SDK sẽ đợi từ 0 đến 30 giây sau khi bạn gọi AdsLoader.requestAds() để thực sự đưa ra yêu cầu đến máy chủ.

Thực tế về việc tìm nạp trước luồng phát trực tiếp

Bạn nên tìm nạp trước điểm chèn quảng cáo tiếp theo ngay khi một điểm chèn quảng cáo hoàn tất. Điều này đảm bảo bạn có thời gian tối đa cho cửa sổ tìm nạp trước. Giả sử bạn có 5 phút giữa các điểm chèn quảng cáo. Khi một điểm chèn quảng cáo kết thúc, bạn có thể yêu cầu điểm chèn quảng cáo tiếp theo với cửa sổ tìm nạp trước là 290 giây (5 phút trừ 10 giây, để đảm bảo các yêu cầu được gửi vào cuối cửa sổ tìm nạp trước có đủ thời gian để phân giải):

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

    ...
  }
}