يمكن استخدام حزمة تطوير البرامج لإعلانات الوسائط التفاعلية لتحقيق الربح من البث المباشر والفيديوهات عند الطلب. بالنسبة إلى البث المباشر، عليك تقديم طلب عرض إعلان جديد لكل فاصل إعلاني. يمكنك توزيع هذه الطلبات لضمان عدم طلب جميع المشاهدين للإعلانات في الوقت نفسه وإبطاء خادم الإعلانات أو الخوادم.
للمساعدة في ذلك، تتضمّن حزمة تطوير البرامج لإعلانات الوسائط التفاعلية السمة AdsRequest.liveStreamPrefetchSeconds. تحدّد هذه السمة الحد الأقصى لعدد الثواني التي يجب أن تنتظرها حزمة تطوير البرامج قبل التواصل مع خادم الإعلانات بعد طلب AdsLoader.requestAds(). سيتم اختيار وقت الطلب عشوائيًا. على سبيل المثال، إذا ضبطت AdsRequest.liveStreamPrefetchSeconds على 30، تنتظر حزمة تطوير البرامج من 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();
}