Media Session action types - Web APIs 编辑

To support an action on a media session, such as seeking, pausing, or changing tracks, you need to call the MediaSession interface's setActionHandler() method to establish a handler for that action. The specific type of media session action to be handled on a MediaSession is identified using a string from the MediaSessionAction enumerated type.

Syntax

A media session action's type is specified using a string from the MediaSessionAction enumerated type.

BCD tables only load in the browser

Each of the actions is a common media session control request. Implement support for each of these in order to allow that type of action to be performed. The following strings identify the currently available types of media session action:

nexttrack
Advances playback to the next track.
pause
Pauses playback of the media.
play
Begins (or resumes) playback of the media.
previoustrack
Moves back to the previous track.
seekbackward
Seeks backward through the media from the current position. The MediaSessionActionDetails property seekOffset specifies the amount of time to seek backward.
seekforward
Seeks forward from the current position through the media. The MediaSessionActionDetails property seekOffset specifies the amount of time to seek forward.
seekto
Moves the playback position to the specified time within the media. The time to which to seek is specified in the MediaSessionActionDetails property seekTime. If you intend to perform multiple seekto operations in rapid succession, you can also specify the MediaSessionActionDetails property fastSeek property with a value of true. This lets the browser know it can take steps to optimize repeated operations, and is likely to result in improved performance.
skipad
Skips past the currently playing advertisement or commercial. This action may or may not be available, depending on the platform and user agent, or may be disabled due to subscription level or other circumstances.
stop
Halts playback entirely.

A media session action may be generated by any media session action source; these sources include anything from UI widgets within the browser itself to media control keys on the user's keyboard to buttons on the user's headset or earbuds.

This example implements seek forward and backward actions for an audio player by setting up the seekforward and seekbackward action handlers.

let skipTime = 10; // Time to skip in seconds

navigator.mediaSession.setActionHandler('seekforward', details => {
 // User clicked "Seek Forward" media notification icon.
 audio.currentTime = Math.min(audio.currentTime + skipTime,
               audio.duration);
});

navigator.mediaSession.setActionHandler('seekbackward', details => {
 // User clicked "Seek Backward" media notification icon.
 audio.currentTime = Math.max(audio.currentTime - skipTime, 0);
});

In this case, both the seekforward and seekbackward handlers are using a fixed distance, ignoring the time specified by the MediaSessionActionDetails passed into the handler.

You can also, if you prefer, use a single function to handle multiple action types, by checking the value of the MediaSessionActionDetails object's action property:

let skipTime = 7;

navigator.mediaSession.setActionHandler("seekforward", handleSeek);
navigator.mediaSession.setActionHandler("seekbackward", handleSeek);

function handleSeek(details) {
  switch(details.action) {
    case "seekforward":
      audio.currentTime = Math.min(audio.currentTime + skipTime,
              audio.duration);
      break;
    case "seekbackward":
      audio.currentTime = Math.max(audio.currentTime - skipTime, 0);
      break;
  }
}

Here, the handleSeek() function handles both seekbackward and seekforward actions.

In this example, instead of seeking a specified distance, the seekforward and seekbackward handlers use the time specified in the MediaSessionActionDetails object's seekOffset property.

navigator.mediaSession.setActionHandler("seekforward", handleSeek);
navigator.mediaSession.setActionHandler("seekbackward", handleSeek);

function handleSeek(details) {
  switch(details.action) {
    case "seekforward":
      audio.currentTime = Math.min(audio.currentTime + details.seekOffset,
                                   audio.duration);
      break;
    case "seekbackward":
      audio.currentTime = Math.max(audio.currentTime - details.seekOffset, 0);
      break;
  }
});
SpecificationStatusComment
Media Session Standard
The definition of 'Media Session action types' in that specification.
DraftInitial definition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:46 次

字数:7202

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文