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

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

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

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

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