Advertising *********** PRESTOplay for Web Apps advertising support. Covers the built-in ads API and plugins for Google Interactive Media Ads (IMA), Yospace, and FreeWheel, plus server-guided ad insertion (SGAI). Code samples: * Ad UI and skipping (https://raw.githubusercontent.com/castlabs/prestoplay-web-code-samples/refs/heads/main/examples/advertising/ad_ui.js): 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: * Google IMA (advertising-ima.txt): Client-side ads with the Google Interactive Media Ads (IMA) plugin: VMAP and VAST ad tags, smart TVs, and iOS. * Yospace (advertising-yospace.txt): Server-side ads with the Yospace plugin: Yospace SDK setup, sessions, and Yospace events. * FreeWheel (beta) (advertising-freewheel.txt): Ads with the FreeWheel AdManager plugin (beta): SDK setup and ad pod configuration. * SGAI (sgai.txt): Server-guided ad insertion (SGAI) with HLS interstitials and DASH alternative MPDs: configuration, Safari support, and the playback session ID. 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()" (https://players.castlabs.com/web/latest/docs/api/clpp.Player.md#getAdsManager) on a "Player" (https://players.castlabs.com/web/latest/docs/api/clpp.Player.md) instance to access "IAdsManager" (https://players.castlabs.com/web/latest/docs/api/clpp.ads.IAdsManager.md). Call this method after the "open()" (https://players.castlabs.com/web/latest/docs/api/clpp.Player.md#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" (https://players.castlabs.com/web/latest/docs/api/clpp.ads.IAd.md)) 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()" (https://players.castlabs.com/web/latest/docs/api/clpp.Player.md#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" (https://players.castlabs.com/web/latest/docs/api/clpp.ads.IAd.md#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()" (https://players.castlabs.com/web/latest/docs/api/clpp.ads.IAd.md#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()" (https://players.castlabs.com/web/latest/docs/api/clpp.ads.IAdsManager.md#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(); });