Caricare gli annunci pre-roll per il live streaming

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.

Per aiutarti in questo, 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 di 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):

// 5 minutes == 300 seconds. Include a 10 second buffer
var AD_INTERVAL = 290;

function onAdEvent(adEvent) {
  var ad = adEvent.getAd();
  switch(adEvent.type) {
    case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
      // Pre-fetch our next ad break.
      requestAds();
      // Play those ads in 5 minutes. In a real-world implementation,
      // this is likely done as the result of a message from your
      // streaming server, not a timeout.
      setTimeout(playAds, AD_INTERVAL * 1000);// Convert to ms.
  }
}

function requestAds() {
  // Destroy the current AdsManager, in case the tag you requested previously
  // contains post-rolls (don't play those now).
  if (adsManager) {
    adsManager.destroy();
  }
  // Your AdsLoader will be set up on page-load. You should re-use the same
  // AdsLoader for every request. For more info on setting up the AdsLoader,
  // see the IMA HTML5 client-side "Set up the IMA SDK" guide:
  // https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/get-started
  if (adsLoader) {
    // Reset the IMA SDK.
    adsLoader.contentComplete();
  }
  var adsRequest = new google.ima.AdsRequest();
  adsRequest.adTagUrl = '...';
  adsRequest.linearAdSlotWidth = <linear_width>;
  adsRequest.linearAdSlotHeight = <linear_height>;
  adsRequest.nonLinearAdSlotWidth = <nonlinear_width>;
  adsRequest.nonLinearAdSlotHeight = <nonlinear_height>;
  adsRequest.liveStreamPrefechSeconds = AD_INTERVAL;
  adsLoader.requestAds(adsRequest);
}

function playAds() {
  adsManager.init(<linear_width>,  <linear_height>);
  adsManager.start();
}