คุณสามารถใช้ IMA SDK เพื่อสร้างรายได้จากไลฟ์สดและวิดีโอออนดีมานด์ สำหรับไลฟ์สด คุณต้องส่งคำขอโฆษณาใหม่สำหรับช่วงพักโฆษณาแต่ละช่วง โดยส่งคำขอเหล่านี้แบบเหลื่อมกันเพื่อให้ผู้ชมทั้งหมดไม่ส่งคำขอโฆษณาในเวลาเดียวกันและทำให้เซิร์ฟเวอร์โฆษณาทำงานช้าลง
IMA SDK มีพร็อพเพอร์ตี้ AdsRequest.liveStreamPrefetchSeconds เพื่อช่วยในเรื่องนี้ พร็อพเพอร์ตี้นี้จะระบุจำนวนวินาทีสูงสุดที่ SDK ควรรอก่อนที่จะติดต่อเซิร์ฟเวอร์โฆษณาหลังจากที่คุณเรียก AdsLoader.requestAds() เวลาส่งคำขอจริงจะสุ่ม ตัวอย่างเช่น หากคุณตั้งค่า AdsRequest.liveStreamPrefetchSeconds เป็น 30 SDK จะรอ 0 ถึง 30 วินาทีหลังจากที่คุณเรียก AdsLoader.requestAds() เพื่อส่งคำขอไปยังเซิร์ฟเวอร์
การดึงข้อมูลล่วงหน้าของไลฟ์สดในทางปฏิบัติ
เราขอแนะนำให้ดึงข้อมูลล่วงหน้าของช่วงพักโฆษณาถัดไปทันทีที่ช่วงพักโฆษณาปัจจุบันสิ้นสุดลง วิธีนี้จะช่วยให้มีระยะเวลาสูงสุดสำหรับหน้าต่างการดึงข้อมูลล่วงหน้า สมมติว่าคุณมีเวลา 5 นาทีระหว่างช่วงพักโฆษณา เมื่อช่วงพักโฆษณาสิ้นสุดลง คุณสามารถขอช่วงพักโฆษณาถัดไปโดยมีหน้าต่างการดึงข้อมูลล่วงหน้า 290 วินาที (5 นาทีลบ 10 วินาที) เพื่อให้แน่ใจว่าคำขอที่ส่งในช่วงท้ายของหน้าต่างการดึงข้อมูลล่วงหน้ามีเวลาเพียงพอในการดำเนินการ
// 5 minutes == 300 seconds. Include a 10 second buffer
var AD_INTERVAL = 290;
function onAdEvent(adEvent) {
var ad = adEvent.getAd();
switch(adEvent.type) {
case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
// Pre-fetch our next ad break.
requestAds();
// Play those ads in 5 minutes. In a real-world implementation,
// this is likely done as the result of a message from your
// streaming server, not a timeout.
setTimeout(playAds, AD_INTERVAL * 1000);// Convert to ms.
}
}
function requestAds() {
// Destroy the current AdsManager, in case the tag you requested previously
// contains post-rolls (don't play those now).
if (adsManager) {
adsManager.destroy();
}
// Your AdsLoader will be set up on page-load. You should re-use the same
// AdsLoader for every request. For more info on setting up the AdsLoader,
// see the IMA HTML5 client-side "Set up the IMA SDK" guide:
// https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/get-started
if (adsLoader) {
// Reset the IMA SDK.
adsLoader.contentComplete();
}
var adsRequest = new google.ima.AdsRequest();
adsRequest.adTagUrl = '...';
adsRequest.linearAdSlotWidth = <linear_width>;
adsRequest.linearAdSlotHeight = <linear_height>;
adsRequest.nonLinearAdSlotWidth = <nonlinear_width>;
adsRequest.nonLinearAdSlotHeight = <nonlinear_height>;
adsRequest.liveStreamPrefechSeconds = AD_INTERVAL;
adsLoader.requestAds(adsRequest);
}
function playAds() {
adsManager.init(<linear_width>, <linear_height>);
adsManager.start();
}