אפשר להשתמש ב-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();
}