Skip to main content

Using Shaka Player

Quickstart

Add your HTML

Shaka Player works by transforming a regular <video> tag into a robust HLS/DASH player. Browser support varies; see https://caniuse.com/stream.

There are three important parts of this code below:

  1. The aspect-ratio on the container. This prevents layout shift while the player loads.
  2. The links. The poster is your thumbnail and the data-src is your video link
  3. Video attributes like controls and playsinline
<div class="shaka-player" style="width:100%; position:relative; aspect-ratio:16/9; background:#000;">
<video
poster="https://worker.antcdn.net/v1/thumbnail/{orgID}/{envID}/{edgeKey}.png?width=1920&timeStamp=30"
data-src="https://worker.antcdn.net/v1/{edgeKey}/master.m3u8"
controls
playsinline
style="position:absolute; inset:0; width:100%; height:100%;"
></video>
</div>

Add your javascript

Shaka player relies on Javascript to work. Below is the minimal javascript/typescript code to use to get started.

import shaka from 'shaka-player/dist/shaka-player.compiled.js';
// Because HLS streaming isn't natively supported by some browsers,
// shaka starts with a polyfill
shaka.polyfill.installAll();
// Transparent error handling for very very old browsers
if (shaka.Player.isBrowserSupported()) {
// There can be multiple shaka player instances on a page,
// so we loop through them
document.querySelectorAll('.shaka-player video[data-src]').forEach(async (video: HTMLVideoElement) => {
// create and configure the player
const player = new shaka.Player();
// Full docs here: https://shaka-player-demo.appspot.com/docs/api/tutorial-config.html
player.configure({
streaming: {
// After seeking, how many seconds should be
// buffered before playing again?
rebufferingGoal: 0.1,
// How many seconds into the future
// should be buffered?
bufferingGoal: 15,
// How many seconds of previous video
// should be kept
bufferBehind: 10, // how much buffer to leave in the past
},
});
try {
await player.attach(video);
await player.load(video.dataset.src as string);
} catch (e) {
console.error('Error loading video', e);
}
});
} else {
console.error('Browser does not support Shaka Player');
}

Demo Video

Here’s what Shaka Player looks like based on the above code

Ads (optional)

If you need client-side ad insertion, use the player’s recommended integration (e.g. Shaka UI build + Google IMA). Keep ad logic separate from your AntCDN playback URL; the playback URL stays https://worker.antcdn.net/v1/{edgeKey}/master.m3u8 (plus ?jwt=... for private).