שליפה מראש של שידור חי

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

כדי לעזור בכך, IMA SDK כולל את AdsRequest.liveStreamPrefetchSeconds לנכס. בנכס הזה מצוין מספר השניות המקסימלי ל-SDK אמור להמתין לפני יצירת קשר עם שרת המודעות לאחר ביצוע השיחה. AdsLoader.requestAds() זמן הבקשה בפועל ייקבע באופן אקראי. עבור לדוגמה, אם מגדירים את הערך של AdsRequest.liveStreamPrefetchSeconds לערך 30, הערך של ה-SDK מחכה 0 עד 30 שניות אחרי הקריאה אל AdsLoader.requestAds() שולחים את הבקשה לשרת.

שליפה מוקדמת של שידורים חיים – בפועל

מומלץ לאחזר מראש את ההפסקה הבאה למודעות ברגע שההפסקה למודעות תסתיים. הדבר מבטיח מהו משך הזמן המקסימלי הזמין לחלון השליפה מראש (pre-fetch). נניח שיש 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();
}