Appearance
Player Methods
The player you get from new JetstreamPlayer(...) is your remote control. Its methods come in two kinds:
- commands – tell the player to do something, and return nothing
- questions – ask the player something, and return a Promise with the answer
Control playback
play() and pause()
Start and pause playback – the same as the visitor pressing the buttons in the player.
Browsers decide when sound may start
Until the visitor has interacted with the page, calling play() with sound on won't start playback. This is the browser's autoplay policy – the same rule YouTube and everyone else follows, and there is no way around it.
A single click inside the player lifts the restriction. And muted playback starts without any interaction at all.
seekTo(seconds)
Jump to a moment in the media:
ts
player.seekTo(90); // jump to 1:30mute() and unmute()
Turn the sound off and back on.
playNext() and playPrev()
Switch to the next or previous item in a playlist. For a single video or audio these do nothing.
Ask the player questions
The answers come from the player itself, so these methods return a Promise – await it:
ts
const paused = await player.isPaused();Questions can time out
If the player doesn't answer within 5 seconds, the Promise rejects instead of hanging forever. If an unanswered question would break your page, wrap the call in try/catch.
isPaused()
Whether playback is currently paused – true or false.
isMuted()
Whether the sound is muted – true or false.
getCurrentTime()
The current playback position, in seconds.
getVideoCurrentTime() is a deprecated alias of this method – it still works, but new code should use getCurrentTime().
getDuration()
The total length of the media, in seconds.
Listen to events
on(event, handler) and off(event, handler?)
Subscribe to player events after the player is created – the runtime counterpart of the events option, which also lists every event and when it fires. Both ways share the same registry, so listeners added either way fire together.
on() registers a listener and returns an unsubscribe function. Multiple listeners can be registered for the same event – they all fire, in registration order.
ts
const off = player.on('timeupdate', (seconds) => {
console.log('current time', seconds);
});
// later – stop listening
off();off() removes listeners by name. Pass the same handler to remove that one listener, or omit it to remove every listener for the event:
ts
const onPlay = () => console.log('playing');
player.on('play', onPlay);
player.off('play', onPlay); // remove just this handler
player.off('play'); // remove all 'play' handlersClean up
dispose()
Disconnects your code from the player and removes all event listeners. Call it when your page removes the player – for example, when the visitor navigates away in a single-page app.