files/en-us/web/api/mediasession/setpositionstate/index.md
{{APIRef("Media Session API")}}
The setPositionState() method of the
{{domxref("MediaSession")}} interface 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
{{domxref("navigator.mediaSession", "mediaSession")}} object.
setPositionState()
setPositionState(stateDict)
stateDict {{optional_inline}}
duration {{optional_inline}}
playbackRate {{optional_inline}}
position {{optional_inline}}
None ({{jsxref("undefined")}}).
duration is missing, negative, or null.position is either negative or greater than duration.playbackRate is zero.Below is a function which updates the position state of the current {{domxref('MediaSession')}} track.
function updatePositionState() {
navigator.mediaSession.setPositionState({
duration: audioEl.duration,
playbackRate: audioEl.playbackRate,
position: audioEl.currentTime,
});
}
We can use this function when updating {{domxref('MediaMetadata')}} 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}}
{{Compat}}