लाइव स्ट्रीम को पहले फ़ेच करें

IMA SDK का इस्तेमाल, लाइव स्ट्रीम के साथ-साथ मांग पर वीडियो से कमाई करने के लिए किया जा सकता है. लाइव स्ट्रीम के लिए, आपको विज्ञापन के लिए हर ब्रेक के लिए नया विज्ञापन अनुरोध करना होगा. यह पक्का करने के लिए इन अनुरोधों को स्टेज करें कि आपके सभी दर्शक एक साथ विज्ञापन का अनुरोध नहीं कर रहे हैं और विज्ञापन सर्वर को ही जाम कर रहे हैं.

इसमें मदद करने के लिए, IMA SDK के पास AdsRequest.liveStreamPrefetchSeconds प्रॉपर्टी मौजूद होती है. इस प्रॉपर्टी से तय होता है कि AdsLoader.requestAds() को कॉल करने के बाद, SDK टूल को विज्ञापन सर्वर से संपर्क करने में ज़्यादा से ज़्यादा कितने सेकंड का इंतज़ार करना चाहिए. अनुरोध करने का असल समय किसी भी क्रम में लगाया जाएगा. उदाहरण के लिए, अगर AdsRequest.liveStreamPrefetchSeconds को 30 पर सेट किया जाता है, तो AdsLoader.requestAds() को कॉल करने के बाद, SDK टूल 0 से 30 सेकंड तक इंतज़ार करता है, ताकि सर्वर को अनुरोध भेजा जा सके.

लाइव स्ट्रीम के लिए प्री-फ़ेच की सुविधा का इस्तेमाल किया जा रहा है

हमारा सुझाव है कि विज्ञापन के लिए ब्रेक खत्म होते ही, विज्ञापन के लिए अगला ब्रेक अपने-आप फ़ेच करें. इससे यह पक्का होता है कि आपकी प्री-फ़ेच विंडो के लिए ज़्यादा से ज़्यादा समयसीमा उपलब्ध है. मान लें कि विज्ञापन के लिए ब्रेक के बीच पांच मिनट का समय होता है. विज्ञापन के लिए ब्रेक की प्रोसेस पूरी हो जाने पर, 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();
}