大网数据广告活动
Web 标准与前端开发

MediaSession: setPositionState() method

· 52CE 我爱测

The **`setPositionState()`** method of the 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 yo

Media Session API

The setPositionState() method of the 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 navigator.mediaSession object.

Syntax

setPositionState()
setPositionState(stateDict)

Parameters

  • stateDict
    • : An object 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. This object can contain the following parameters:
      • duration
        • : A floating-point value giving the total duration of the current media in seconds. This should always be a positive number, with positive infinity (Infinity) indicating media without a defined end, such as a live stream.
      • playbackRate
        • : A floating-point value indicating the rate at which the media is being played, as a ratio relative to its normal playback speed. Thus, a value of 1 is playing at normal speed, 2 is playing at double speed, and so forth. Negative values indicate that the media is playing in reverse; -1 indicates playback at the normal speed but backward, -2 is double speed in reverse, and so on.
      • position
        • : A floating-point value indicating the last reported playback position of the media in seconds. This must always be a positive value.

Return value

None (undefined).

Exceptions

  • TypeError
    • : This error can occur in an array of circumstances:
      • The specified object's duration is missing, negative, or null.
      • Its position is either negative or greater than duration.
      • Its playbackRate is zero.

Examples

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 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

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。