คุณสามารถใช้ 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 "Get Started" guide in the prerequisites above.
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>, google.ima.ViewMode.NORMAL);
adsManager.start();
}