تبلیغات پیش از پخش را برای پخش زنده بارگیری کنید

پلتفرم مورد نظر را انتخاب کنید: HTML5 اندروید iOS tvOS

IMA SDK می‌تواند برای کسب درآمد از پخش زنده و همچنین ویدیوی درخواستی استفاده شود. برای پخش زنده، باید برای هر وقفه تبلیغاتی، درخواست تبلیغ جدیدی ارسال کنید. این درخواست‌ها را به صورت متناوب ارسال کنید تا مطمئن شوید که همه بینندگان شما همزمان درخواست تبلیغات نمی‌کنند و سرور(های) تبلیغاتی را با مشکل مواجه نمی‌کنند.

برای کمک به این امر، IMA SDK دارای ویژگی AdsRequest.liveStreamPrefetchSeconds است. این ویژگی حداکثر تعداد ثانیه‌هایی را که SDK باید قبل از تماس با سرور تبلیغ پس از فراخوانی AdsLoader.requestAds() منتظر بماند، مشخص می‌کند. زمان درخواست واقعی تصادفی خواهد بود. برای مثال، اگر AdsRequest.liveStreamPrefetchSeconds روی 30 تنظیم کنید، SDK پس از فراخوانی AdsLoader.requestAds() برای ارسال درخواست به سرور، 0 تا 30 ثانیه منتظر می‌ماند.

پیش‌دریافت پخش زنده در عمل

توصیه می‌کنیم به محض اتمام یک تبلیغ، پیش‌دریافت (prefetch) تبلیغ بعدی خود را انجام دهید. این کار تضمین می‌کند که حداکثر مدت زمان موجود برای پنجره پیش‌دریافت شما وجود دارد. فرض کنید ۵ دقیقه بین هر تبلیغ فاصله دارید. وقتی یک تبلیغ کامل شد، می‌توانید تبلیغ بعدی خود را با یک پنجره پیش‌دریافت ۲۹۰ ثانیه‌ای (۵ دقیقه منهای ۱۰ ثانیه، برای اطمینان از اینکه درخواست‌های ارسالی در انتهای پنجره پیش‌دریافت، زمان کافی برای حل و فصل دارند) درخواست کنید:

هدف-سی


- (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;

    ...

  }

  ...

}

سویفت

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

    ...
  }
}