L'SDK IMA può essere utilizzato per monetizzare i live streaming e i video on demand. Per i live streaming, devi effettuare una nuova richiesta di annuncio per ogni interruzione pubblicitaria. Scagliona queste richieste per assicurarti che non tutti gli spettatori richiedano annunci contemporaneamente, sovraccaricando gli ad server.
Per aiutarti, l'SDK IMA dispone della proprietà AdsRequest.liveStreamPrefetchSeconds. Questa proprietà specifica il numero massimo di secondi che l'SDK deve attendere prima di contattare l'ad server dopo la chiamata di AdsLoader.requestAds(). L'ora effettiva della richiesta verrà randomizzata. Ad esempio, se imposti AdsRequest.liveStreamPrefetchSeconds su 30, l'SDK attende da 0 a 30 secondi dopo la chiamata di AdsLoader.requestAds() per effettuare effettivamente la richiesta al server.
Precaricamento del live streaming in pratica
Ti consigliamo di precaricare l'interruzione pubblicitaria successiva non appena ne viene completata una. In questo modo, la finestra di precaricamento avrà la durata massima possibile. Supponiamo che tra un'interruzione pubblicitaria e l'altra trascorrano 5 minuti. Al termine di un'interruzione pubblicitaria, puoi richiedere l'interruzione pubblicitaria successiva con una finestra di precaricamento di 290 secondi (5 minuti meno 10 secondi, per assicurarti che le richieste inviate alla fine della finestra di precaricamento abbiano tempo sufficiente per essere risolte):
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
...
}
}