MediaSession.setPositionState()

The MediaSession method setPositionState() is used to update the current document's media playback position and speed for presentation by user's device in any kind of interface that provides details about ongoing media. This can be particularly useful if your code implements a player for type of media not directly supported by the browser.

Call this method on the navigator object's mediaSession object.

Syntax

navigator.mediaSession.setPositionState(stateDict);

Parameters

stateDict Optional
An object conforming to the MediaPositionState dictionary, providing updated information about the playback position and speed of the document's ongoing media. If the object is empty, the existing playback state information is cleared.

Return value

undefined.

Exceptions

TypeError

This error can occur in an array of circumstances:

Example

In this example, a player for a non-standard media file format, written in JavaScript, uses setInterval() to establish a callback which fires once per second to refresh the position information by calling setPositionState(). If the media is still playing when the interval is fired, setPositionState() is called with an object that specifies the duration, playback rate, and position as reported by a myMedia object that describes the track being played.

If the media is not playing, clearInterval() is used to remove the interval handler.

let positionInterval = window.setInterval(() => {
  if (myMedia.isPlaying) {
    navigator.mediaSession.setPositionState({
      duration: myMedia.trackDurationInSeconds,
      playbackRate: myMedia.playbackRate,
      position: myMedia.trackPlayPositionInSeconds
    });
  } else {
    window.clearInterval(positionInterval);
  }
}, 1000);

Specifications

Specification Status Comment
Media Session Standard
The definition of 'MediaSession.setPositionState()' in that specification.
Draft Initial definition.

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
setPositionState()
Experimental
Chrome Full support 73Edge Full support ≤79Firefox No support NoIE No support NoOpera Full support YesSafari ? WebView Android No support NoChrome Android Full support 57Firefox Android No support NoOpera Android No support NoSafari iOS ? Samsung Internet Android Full support 7.0

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.