El SDK de IMA se puede usar para monetizar transmisiones en vivo y videos on demand. En el caso de las transmisiones en vivo, debes realizar una nueva solicitud de anuncio para cada pausa publicitaria. Escalona estas solicitudes para garantizar que no todos los usuarios soliciten anuncios al mismo tiempo y sobrecarguen los servidores de anuncios.
Para ayudarte con esto, el SDK de IMA tiene la propiedad AdsRequest.liveStreamPrefetchSeconds. Esta propiedad especifica la cantidad máxima de segundos que el SDK debe esperar antes de comunicarse con el servidor de anuncios después de que llamas a AdsLoader.requestAds(). La hora real de la solicitud se asignará de forma aleatoria. Por
ejemplo, si configuras AdsRequest.liveStreamPrefetchSeconds en 30, el SDK
esperará entre 0 y 30 segundos después de que llames a AdsLoader.requestAds() para realizar
la solicitud al servidor.
La carga previa de transmisiones en vivo en la práctica
Te recomendamos que recuperes previamente la siguiente pausa publicitaria en cuanto se complete la anterior. Esto garantiza que la ventana de carga previa tenga disponible el período máximo. Supongamos que tienes 5 minutos entre las pausas publicitarias. Cuando se completa una pausa publicitaria, puedes solicitar la siguiente con una ventana de carga previa de 290 segundos (5 minutos menos 10 segundos, para asegurarte de que las solicitudes enviadas al final de la ventana de carga previa tengan tiempo suficiente para resolverse):
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
...
}
}