Tìm nạp trước luồng trực tiếp

Bạn có thể sử dụng SDK IMA để kiếm tiền từ các sự kiện phát trực tiếp cũng như video theo yêu cầu. Đối với sự kiện phát trực tiếp, bạn cần tạo một yêu cầu quảng cáo mới cho mỗi điểm chèn quảng cáo. Phân chia các yêu cầu này để đảm bảo rằng tất cả người xem của bạn không yêu cầu quảng cáo cùng một lúc và làm hỏng(các) máy chủ quảng cáo.

Để giúp giải quyết việc này, SDK IMA có thuộc tính AdsRequest.liveStreamPrefetchSeconds. Thuộc tính này chỉ định số giây tối đa mà SDK cần chờ trước khi liên hệ với máy chủ quảng cáo sau khi bạn gọi AdsLoader.requestAds(). Thời gian yêu cầu thực tế sẽ được chọn ngẫu nhiên. Ví dụ: nếu bạn đặt AdsRequest.liveStreamPrefetchSeconds thành 30, SDK sẽ đợi từ 0 đến 30 giây sau khi bạn gọi AdsLoader.requestAds() để thực sự gửi yêu cầu đến máy chủ.

Tính năng tìm nạp trước sự kiện phát trực tiếp trong thực tế

Bạn nên tìm nạp trước điểm chèn quảng cáo tiếp theo ngay khi điểm chèn quảng cáo hoàn tất. Điều này đảm bảo khoảng thời gian tối đa có sẵn cho cửa sổ tìm nạp trước của bạn. Giả sử bạn có 5 phút giữa các điểm chèn quảng cáo. Khi một điểm chèn quảng cáo hoàn tất, bạn có thể yêu cầu điểm chèn quảng cáo tiếp theo với thời lượng tìm nạp trước là 290 giây (5 phút trừ 10 giây, để đảm bảo các yêu cầu được gửi ở cuối cửa sổ tìm nạp trước có đủ thời gian để giải quyết):

Các đoạn mã này cho biết cách thêm tính năng tìm nạp trước chương trình phát trực tiếp vào Ví dụ nâng cao, nhưng phương pháp này có thể áp dụng cho các phương thức triển khai IMA khác.

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);
}