Caricare gli annunci pre-roll per il live streaming

l'SDK IMA (Interactive Media Ads) di Google per iOS.

L'SDK IMA può essere utilizzata per monetizzare sia i live streaming sia i video on demand. Per i live streaming, devi effettuare una nuova richiesta di annuncio per ogni interruzione pubblicitaria. Scaglionare queste richieste per assicurarti che tutti gli spettatori non richiedano gli annunci contemporaneamente e non sovraccarichino gli ad server.

A questo scopo, 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 aver chiamato AdsLoader.requestAds(). L'ora effettiva della richiesta verrà scelta in modo casuale. Ad esempio, se imposti AdsRequest.liveStreamPrefetchSeconds su 30, l'SDK attende da 0 a 30 secondi dopo la chiamata a AdsLoader.requestAds() per effettuare la richiesta al server.

Pre-fetch dei live streaming in pratica

Ti consigliamo di precaricare la pausa pubblicitaria successiva non appena una pausa pubblicitaria termina. In questo modo, la durata massima di tempo è disponibile per la finestra di precaricamento. Supponiamo che tu abbia 5 minuti tra le interruzioni pubblicitarie. Al termine di un'interruzione pubblicitaria, puoi richiedere la 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 abbastanza tempo 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

    ...
  }
}