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