การดึงข้อมูลสตรีมแบบสดล่วงหน้า

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