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
Exceptions
TypeError
- This error can occur in an array of circumstances:
- The specified
MediaPositionState
object'sduration
is missing, negative, ornull
. position
is missing or null, or is either negative or greater thanduration
.- The
playbackRate
is zero.
- The specified
Example
Below is a function which updates the position state of the current MediaSession
track.
function updatePositionState() {
navigator.mediaSession.setPositionState({
duration: audioEl.duration,
playbackRate: audioEl.playbackRate,
position: audioEl.currentTime
});
}
We can use this function when updating media session metadata
and within callbacks for actions, such as below.
navigator.mediaSession.setActionHandler('seekbackward', details => {
// our time to skip
const skipTime = details.seekOffset || 10;
// set our position
audioEl.currentTime = Math.max(audioEl.currentTime - skipTime, 0);
updatePositionState();
});
Specifications
Specification | Status | Comment |
---|---|---|
Media Session StandardThe definition of 'MediaSession.setPositionState()' in that specification. | Draft | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Chrome
Full support 73 |
Edge
Full support ≤79 |
Firefox Full support 76 Full support 76 Disabled' From version 76: this feature is behind the |
IE
No support No |
Opera
Full support Yes |
Safari
No support No |
WebView Android
No support No |
Chrome Android
Full support 57 |
Firefox Android
No support No |
Opera Android
No support No |
Safari iOS
No support No |
Samsung Internet Android
Full support 7.0 |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.'
- Experimental. Expect behavior to change in the future.
- User must explicitly enable this feature.'
- User must explicitly enable this feature.
MediaSession.setPositionState() by Mozilla Contributors is licensed under CC-BY-SA 2.5.