लाइव स्ट्रीम के लिए, वीडियो शुरू होने से पहले दिखने वाले विज्ञापन लोड करना

प्लैटफ़ॉर्म चुनें: HTML5 Android iOS tvOS

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

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

लाइव स्ट्रीम को पहले से फ़ेच करने की सुविधा का इस्तेमाल करना

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