ניתן להשתמש ב-IMA SDK כדי לייצר הכנסות משידורים חיים ומשידורים חיים על פי דרישה. בשידורים חיים, צריך לשלוח בקשה חדשה להצגת מודעה בכל הפסקה למודעות. צריך להזיז את הבקשות האלה כדי לוודא שכל הצופים לא יבקשו את המודעות בו-זמנית ו"לאתגר" את שרתי המודעות.
כדי לעזור בכך, IMA SDK כולל את AdsRequest.liveStreamPrefetchSeconds
לנכס. בנכס הזה מצוין מספר השניות המקסימלי ל-SDK
אמור להמתין לפני יצירת קשר עם שרת המודעות לאחר ביצוע השיחה.
AdsLoader.requestAds()
זמן הבקשה בפועל ייקבע באופן אקראי. עבור
לדוגמה, אם מגדירים את הערך של AdsRequest.liveStreamPrefetchSeconds
לערך 30, הערך של ה-SDK
מחכה 0 עד 30 שניות אחרי הקריאה אל AdsLoader.requestAds()
שולחים את הבקשה לשרת.
שליפה מוקדמת של שידורים חיים – בפועל
מומלץ לאחזר מראש את ההפסקה הבאה למודעות ברגע שההפסקה למודעות תסתיים. הדבר מבטיח מהו משך הזמן המקסימלי הזמין לחלון השליפה מראש (pre-fetch). נניח שיש 5 דקות בין ההפסקות למודעות. כשהפסקה למודעה מסתיימת, אפשר לבקש את ההפסקה הבאה למודעה עם חלון שליפה מראש של 290 שניות (5 דקות פחות 10 שניות, כדי לוודא שהבקשות נשלחו בסוף לחלון השליפה מראש יש מספיק זמן כדי לפתור את הבעיה):
Objective-C
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {
...
switch (event.type) {
...
case kIMAAdEvent_ALL_ADS_COMPLETED:
IMAAdsRequest *request = [[IMAAdsRequest alloc]
initWithAdTagUrl: self.adTagUrl
adDisplayContainer: self.adDisplayContainer
avPlayerVideoDisplay: self.avPlayerVideoDisplay
pictureInPictureProxy: self.pictureInPictureProxy
userContext: nil];
// set a delay between the end of the last ad
// in the last request, and the first ad from
// the new request
Float64 adGap = 30;
// make sure the request occurs at least five
// seconds before starting the new set of ads
request.liveStreamPrefetchSeconds = adGap - 5;
[self.adsLoader requestAdsWithRequest:request];
// start new ads after adGap seconds have elapsed
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, adGap * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[adsManager start];
});
break;
...
}
...
}
Swift
func adsManager(_ adsManager: IMAAdsManager!, didReceive event: IMAAdEvent!) {
switch event.type {
...
case IMAAdEventType.ALL_ADS_COMPLETED:
let request = IMAAdsRequest(
adTagUrl: AdTagUrl,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: nil)
// set a delay between the end of the last ad
// in the last request, and the first ad from
// the new request
let adGap = 30
// make sure the request occurs at least five
// seconds before starting the new set of ads
request.liveStreamPrefetchSeconds = adGap - 5
adsLoader.requestAds(with: request)
// start new ads after adGap seconds have elapsed
DispatchQueue.main.asyncAfter(deadline: .now() + adGap) {
adsManager.start()
}
break
...
}
}