Precaricare le interruzioni pubblicitarie per gli stream

Seleziona la piattaforma: HTML5 Android iOS tvOS

L'SDK IMA può essere utilizzato per monetizzare sia i live streaming sia i video on demand. Per i live streaming, devi effettuare una nuova richiesta di annunci per ogni interruzione pubblicitaria. Scaglionale per assicurarti che tutti gli spettatori non richiedano annunci contemporaneamente, sovraccaricando gli ad server.

A questo scopo, l'SDK IMA ha la 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(). Il tempo di richiesta effettivo verrà randomizzato. Ad esempio, se imposti AdsRequest.liveStreamPrefetchSeconds su 30, l'SDK attende da 0 a 30 secondi dopo aver chiamato AdsLoader.requestAds() per effettuare la richiesta al server.

Precaricamento dei live streaming in pratica

Ti consigliamo di precaricare la prossima interruzione pubblicitaria non appena ne viene completata una. In questo modo, la finestra di precaricamento avrà la durata massima. Supponiamo che tu abbia 5 minuti tra le interruzioni pubblicitarie. Quando un'interruzione pubblicitaria viene completata, 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 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

    ...
  }
}