Advertising

PRESTOplay for Web Apps includes a built-in ads API plus ready-to-use plugins for popular ad providers, so you can insert client-side and server-side ads without building ad tracking from scratch.

Code samples

  • Ad UI and skipping: a custom player UI that reacts to ads, with an ad label, the time left, and a skip button.

PRESTOplay for Web Apps includes integrations for Google Interactive Media Ads (IMA), Yospace, and FreeWheel AdManager, plus support for server-guided ad insertion (SGAI) with HLS interstitials and DASH alternative MPDs. Each has its own page:

The Broadpeak plugin also supports server-side ad insertion (SSAI) tracking. See Broadpeak SSAI ads.

All ad integrations share the ads API and ad events below.

Ads API

Ads manager

Call getAdsManager() on a Player instance to access IAdsManager. Call this method after the open() promise resolves. From that point, you can control the currently playing ad.

Ad events

When ads are active, the player emits ad-related events. Attach ad event listeners to the player instance:

player.on(clpp.events.ADS_TIMELINE_CHANGED, event => {
  console.log('==> ad-timeline-changed', event.adsTimeline.getCuePoints());
});

player.on(clpp.events.AD_LOADED, event => {
  console.log('==> ad-loaded', event.ad);
});

Useful ad events include:

  • clpp.events.ADS_TIMELINE_CHANGED

  • clpp.events.AD_LOADED

  • clpp.events.AD_BREAK_STARTED

  • clpp.events.AD_STARTED

  • clpp.events.AD_BUFFERING

  • clpp.events.AD_PROGRESS

  • clpp.events.AD_FIRST_QUARTILE

  • clpp.events.AD_MIDPOINT

  • clpp.events.AD_THIRD_QUARTILE

  • clpp.events.AD_PAUSED

  • clpp.events.AD_RESUMED

  • clpp.events.AD_SKIPPED

  • clpp.events.AD_CLICKED

  • clpp.events.AD_IMPRESSION

  • clpp.events.AD_COMPLETED

  • clpp.events.AD_STOPPED

  • clpp.events.AD_BREAK_STOPPED

See Events for general event-handling patterns.

Rendering UI

To render UI related to ads, listen to ad events and use the event payload (IAd) to get information about the currently playing ad.

player.on(clpp.events.AD_STARTED, event => {
  const position = event.ad.getPositionInSequence();
  const count = event.ad.getSequenceLength();
  adLabel = `Ad ${position} / ${count}`;
});

player.on(clpp.events.AD_BREAK_STOPPED, () => {
  adLabel = null;
});

To render a play/pause button, listen to play and pause events as well as ad paused, resumed, and started. In all these cases the return value of isPaused() may change.

function renderPlayButton() {
  if (player.isPaused()) {
    playButton.textContent = 'Play';
  } else {
    playButton.textContent = 'Pause';
  }
}

player.on('play', () => {
  renderPlayButton();
});
player.on('pause', () => {
  renderPlayButton();
});
player.on(clpp.events.AD_STARTED, (e) => {
  renderPlayButton();
});
player.on(clpp.events.AD_PAUSED, () => {
  renderPlayButton();
});
player.on(clpp.events.AD_RESUMED, () => {
  renderPlayButton();
});

Seek UI during ads

Some ad formats do not allow seeking. Use canSeek() on the current ad (IAd#canSeek) to determine whether the seek bar and any other seek or playback rate controls should be enabled.

function renderSeekBar(ad) {
  const seekEnabled = ad ? ad.canSeek() : true;
  seekBar.disabled = !seekEnabled;
  playbackRateSelect.disabled = !seekEnabled;
  // Hide or disable any other seek or playback rate controls accordingly.
}

player.on(clpp.events.AD_STARTED, event => {
  // Check and possibly disable seeking when the ad starts.
  renderSeekBar(event.ad);
});

player.on(clpp.events.AD_BREAK_STOPPED, () => {
  // Re-enable seek when the ad break ends.
  renderSeekBar(null);
});

Skippable ads

Use getSkipTimeOffset() to determine whether an ad is skippable. It returns the number of seconds of playback before the ad can be skipped, or -1 if the ad is not skippable.

To skip the current ad, call skip() on the ads manager.

Here’s an example of rendering a skip button in the UI:

let currentAd = null;

player.on(clpp.events.AD_STARTED, event => {
  currentAd = event.ad;
  renderSkipButton();
});

player.on(clpp.events.AD_PROGRESS, () => {
  renderSkipButton();
});

player.on(clpp.events.AD_BREAK_STOPPED, () => {
  currentAd = null;
  skipButton.style.display = 'none';
});

function renderSkipButton() {
  if (!currentAd) return;
  const skipOffset = currentAd.getSkipTimeOffset();
  if (skipOffset === -1) {
    // Ad is not skippable.
    skipButton.style.display = 'none';
    return;
  }
  const remaining = skipOffset - currentAd.getPosition();
  if (remaining <= 0) {
    skipButton.textContent = 'Skip Ad';
    skipButton.disabled = false;
  } else {
    skipButton.textContent = `Skip in ${Math.ceil(remaining)}s`;
    skipButton.disabled = true;
  }
  skipButton.style.display = 'block';
}

skipButton.addEventListener('click', () => {
  player.getAdsManager().skip();
});
Next topic: Google IMA
Previous topic: Plugins