تحميل فواصل إعلانية مسبقًا لأحداث البث

اختيار النظام الأساسي: HTML5 Android iOS tvOS

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

للمساعدة في ذلك، تتضمّن حزمة تطوير البرامج للإعلانات التفاعلية (IMA SDK) السمة 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 IMA HTML5 client-side "Set up the IMA SDK" guide:
  // https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/get-started
  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>);
  adsManager.start();
}