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

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

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

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

हमारा सुझाव है कि विज्ञापन के लिए ब्रेक खत्म होते ही, विज्ञापन के लिए अगला ब्रेक अपने-आप फ़ेच करें. इससे यह पक्का होता है कि आपकी प्री-फ़ेच विंडो के लिए ज़्यादा से ज़्यादा समयसीमा उपलब्ध है. मान लें कि विज्ञापन के लिए ब्रेक के बीच पांच मिनट का समय होता है. विज्ञापन के लिए ब्रेक की प्रोसेस पूरी हो जाने पर, 290 सेकंड की प्री-फ़ेच विंडो से अगले विज्ञापन ब्रेक का अनुरोध किया जा सकता है (5 मिनट माइनस 10 सेकंड का इस्तेमाल करके, यह पक्का करने के लिए कि प्री-फ़ेच विंडो के आखिर में भेजे गए अनुरोधों के समाधान के लिए काफ़ी समय है):

इन कोड स्निपेट से पता चलता है कि ऐडवांस उदाहरण में, लाइव स्ट्रीम प्री-फ़ेच को कैसे जोड़ा जा सकता है. हालांकि, अन्य IMA लागू करने के तरीकों पर भी यह तरीका लागू किया जा सकता है.

VideoPlayerController.java

/** Ads logic for handling the IMA SDK integration code and events. */
public class VideoPlayerController {

  // 5 minutes == 300 seconds. Include a 10 second buffer
  private float AD_INTERVAL = 290;
  private double AD_TIMEOUT = 300;

...

  adsManager.addAdEventListener(
    new AdEvent.AdEventListener() {
      /** Responds to AdEvents. */
      @Override
      public void onAdEvent(AdEvent adEvent) {

      ...

      case ALL_ADS_COMPLETED:
        if (adsManager != null) {
          adsManager.destroy();
          adsManager = null;
        }

        // When pre-fetching for live streams, be sure to destroy the current AdsManager,
        // in case the tag you requested previously contains post-rolls
        // (you don't want to play those now).

        // Pre-fetch the next ad break.
        // Play those ads in ~5 minutes. In a real-world implementation,
        // this will likely be done as the result of a message from your
        // streaming server, not a via the playAdsAfterThisTime parameter
        // of requestAndPlayAds().
        requestAndPlayAds(AD_TIMEOUT);
        break;
      default:
        break;
      }
  }

...

public void requestAndPlayAds(double playAdsAfterThisTime) {
  if (currentAdTagUrl == null || currentAdTagUrl == "") {
    log("No VAST ad tag URL specified");
    resumeContent();
    return;
  }

  // Since you're switching to a new video, tell the SDK the previous video is finished.
  if (adsManager != null) {
    adsManager.destroy();
  }

  playButton.setVisibility(View.GONE);

  // Create the ads request.
  AdsRequest request = sdkFactory.createAdsRequest();
  request.setAdTagUrl(currentAdTagUrl);
  request.setContentProgressProvider(videoPlayerWithAdPlayback.getContentProgressProvider());
  request.setLiveStreamPrefetchSeconds(AD_INTERVAL);

  playAdsAfterTime = playAdsAfterThisTime;

  // Request the ad. After the ad is loaded, onAdsManagerLoaded() will be called.
  adsLoader.requestAds(request);
}