טעינת מודעות לפני סרטון (pre-roll) לשידור חי

בחירת פלטפורמה: HTML5 Android iOS tvOS

אפשר להשתמש ב-IMA SDK כדי לייצר הכנסות משידורים חיים ומסרטונים על פי דרישה (VOD). בשידורים חיים, צריך לשלוח בקשה חדשה להצגת מודעה בכל הפסקה למודעה. כדי למנוע מצב שבו כל הצופים שולחים בקשות להצגת מודעות בו-זמנית וגורמים לעומס על שרת או שרתי המודעות, כדאי לשלוח את הבקשות האלה בהפרש זמן.

כדי לעזור בכך, ב-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();
}