Precaricamento dei live streaming

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. Distribuite queste richieste in modo scaglionato per garantire che tutti i visualizzatori non richiedano annunci contemporaneamente e non blocchino 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 la chiamata a AdsLoader.requestAds(). L'ora effettiva della richiesta sarà 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 effettivamente la richiesta al server.

Precaricamento del live streaming in pratica

Ti consigliamo di precaricare la prossima interruzione pubblicitaria al termine di un'interruzione pubblicitaria. In questo modo si assicura che sia disponibile la durata massima per la finestra di precaricamento. Supponiamo che ci siano 5 minuti di tempo tra un'interruzione pubblicitaria e l'altra. Al completamento 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

    ...
  }
}