الجلب المسبق للبث المباشر

يمكن استخدام حزمة تطوير البرامج لإعلانات الوسائط التفاعلية لتحقيق ربح من أحداث البث المباشر والفيديوهات عند الطلب. بالنسبة إلى أحداث البث المباشر، عليك تقديم طلب إعلان جديد لكل فاصل إعلاني. يمكنك ترتيب هذه الطلبات للتأكّد من أنّ جميع المشاهدين لا يطلبون الإعلانات في الوقت نفسه وأنّهم يعيقون أداء خوادم الإعلانات.

للمساعدة في ذلك، تحتوي حزمة تطوير البرامج لإعلانات الوسائط التفاعلية على السمة AdsRequest.liveStreamPrefetchSeconds. تحدّد هذه السمة الحد الأقصى لعدد الثواني التي يجب أن تنتظرها حزمة SDK قبل الوصول إلى خادم الإعلانات بعد طلب البيانات AdsLoader.requestAds(). سيتم توزيع وقت الطلب الفعلي عشوائيًا. على سبيل المثال، في حال ضبط AdsRequest.liveStreamPrefetchSeconds على 30، تنتظر حزمة SDK مدة تتراوح بين 0 و30 ثانية بعد استدعاء AdsLoader.requestAds() لإرسال الطلب إلى الخادم.

التطبيق العملي على الجلب المُسبَق للبث المباشر

ننصحك بجلب الفاصل الإعلاني التالي مسبقًا فور اكتماله. يضمن ذلك الحدّ الأقصى لطول مدة نافذة الجلب المسبق. لنفرض أنّ هناك 5 دقائق بين الفواصل الإعلانية. عند اكتمال الفاصل الإعلاني، يمكنك طلب الفاصل الإعلاني التالي باستخدام نافذة للجلب المسبق تبلغ 290 ثانية (5 دقائق ناقص 10 ثوانٍ، للتأكد من أنّ الطلبات المرسَلة في نهاية نافذة الجلب المسبق لديها وقت كافٍ للحلّ):

// 5 minutes == 300 seconds. Include a 10 second buffer
var AD_INTERVAL = 290;

function onAdEvent(adEvent) {
  var ad = adEvent.getAd();
  switch(adEvent.type) {
    case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
      // Pre-fetch our next ad break.
      requestAds();
      // Play those ads in 5 minutes. In a real-world implementation,
      // this is likely done as the result of a message from your
      // streaming server, not a timeout.
      setTimeout(playAds, AD_INTERVAL * 1000);// Convert to ms.
  }
}

function requestAds() {
  // Destroy the current AdsManager, in case the tag you requested previously
  // contains post-rolls (don't play those now).
  if (adsManager) {
    adsManager.destroy();
  }
  // Your AdsLoader will be set up on page-load. You should re-use the same
  // AdsLoader for every request. For more info on setting up the AdsLoader,
  // see the "Get Started" guide in the prerequisites above.
  if (adsLoader) {
    // Reset the IMA SDK.
    adsLoader.contentComplete();
  }
  var adsRequest = new google.ima.AdsRequest();
  adsRequest.adTagUrl = '...';
  adsRequest.linearAdSlotWidth = <linear_width>;
  adsRequest.linearAdSlotHeight = <linear_height>;
  adsRequest.nonLinearAdSlotWidth = <nonlinear_width>;
  adsRequest.nonLinearAdSlotHeight = <nonlinear_height>;
  adsRequest.liveStreamPrefechSeconds = AD_INTERVAL;
  adsLoader.requestAds(adsRequest);
}

function playAds() {
  adsManager.init(
      <linear_width>,  <linear_height>, google.ima.ViewMode.NORMAL);
  adsManager.start();
}