โหลดโฆษณาตอนต้นสำหรับสตรีมแบบสด

เลือกแพลตฟอร์ม HTML5 Android iOS tvOS

คุณใช้ IMA SDK เพื่อสร้างรายได้จากไลฟ์สดและวิดีโอออนดีมานด์ได้ สำหรับไลฟ์สด คุณต้องส่งคำขอโฆษณาใหม่สำหรับช่วงพักโฆษณาแต่ละช่วง คุณควรส่งคำขอเหล่านี้แบบเหลื่อมกันเพื่อให้ผู้ชมทั้งหมดไม่ส่งคำขอ โฆษณาพร้อมกันและทำให้เซิร์ฟเวอร์โฆษณาทำงานช้าลง

IMA SDK มีAdsRequest.liveStreamPrefetchSeconds เพื่อช่วยในเรื่องนี้ พร็อพเพอร์ตี้นี้ระบุจำนวนวินาทีสูงสุดที่ SDK ควรรอก่อนที่จะติดต่อเซิร์ฟเวอร์โฆษณาหลังจากที่คุณเรียกใช้ AdsLoader.requestAds() ระบบจะสุ่มเวลาคำขอจริง ตัวอย่างเช่น หากตั้งค่า AdsRequest.liveStreamPrefetchSeconds เป็น 30 SDK จะรอ 0-30 วินาทีหลังจากที่คุณเรียกใช้ AdsLoader.requestAds() เพื่อส่งคำขอไปยังเซิร์ฟเวอร์จริงๆ

การดึงข้อมูลล่วงหน้าของไลฟ์สดในทางปฏิบัติ

เราขอแนะนำให้ดึงข้อมูลล่วงหน้าสำหรับช่วงพักโฆษณาถัดไปทันทีที่ช่วงพักโฆษณาสิ้นสุดลง ซึ่งจะช่วยให้มีระยะเวลาสูงสุดสำหรับหน้าต่างการดึงข้อมูลล่วงหน้า สมมติว่าคุณมีเวลา 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 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);
}