MediaSession 方法 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.

调用此方法在 navigator 对象的 mediaSession 对象。

句法

navigator.mediaSession.setPositionState(stateDict);
					

参数

stateDict 可选
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.

返回值

undefined .

异常

TypeError

This error can occur in an array of circumstances:

范例

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

规范

规范 状态 注释
Media Session Standard
The definition of 'MediaSession.setPositionState()' in that specification.
草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
setPositionState() Chrome 73 Edge ≤79 Firefox No IE No Opera Yes Safari ? WebView Android No Chrome Android 57 Firefox Android No Opera Android No Safari iOS ? Samsung Internet Android 7.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

元数据

  • 最后修改:
  1. 媒体会话 API
  2. MediaSession
  3. 特性
    1. metadata
    2. playbackState
  4. 方法
    1. setActionHandler()
    2. setPositionState()
  5. Related pages for Media Session API
    1. MediaMetadata