setActionHandler() 特性为 MediaSession interface sets an event handler for a media session action. These actions let a web app receive notifications when the user engages a device's built-in physical or onscreen media controls, such as play, stop, or seek buttons.

句法

navigator.mediaSession.setActionHandler(type, callback)
					

参数

type
DOMString representing an action type to listen for. It will be one of play , pause , seekbackward , seekforward , seekto , skipad , previoustrack ,或 nexttrack . Further details on the action types can be found below under Media session actions .
callback

A function to call when the specified action type is invoked. The callback receives no input parameters, and should not return a value.

返回值

undefined .

描述

To remove a previously-established action handler, call setActionHandler() again, specifying null 作为 callback .

The action handler receives as input a single parameter: an object conforming to the MediaSessionActionDetails dictionary, which provides both the action type (so the same function can handle multiple action types), as well as data needed in order to perform the action.

Media session actions

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

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

暂停媒体回放。

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.

seekforward

Seeks forward from the current position through the media.

seekto

Moves the playback position to the specified time within the media.

skipad
Skips past the currently playing advertisement or commercial. This action may or may not be available, depending on the platform and 用户代理 , or may be disabled due to subscription level or other circumstances.
stop

Halts playback entirely.

范例

Adding action handlers

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', evt => {
 // User clicked "Seek Forward" media notification icon.
 audio.currentTime = Math.min(audio.currentTime + skipTime,
               audio.duration);
});
navigator.mediaSession.setActionHandler('seekbackward', evt => {
 // User clicked "Seek Backward" media notification icon.
 audio.currentTime = Math.max(audio.currentTime - skipTime, 0);
});
					

Supporting multiple actions in one handler function

You can also, if you prefer, use a single function to handle multiple action types, by checking the value of the MediaSessionActionDetails 对象的 action 特性:

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

在这里, handleSeek() function handles both seekbackward and seekforward actions.

规范

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

浏览器兼容性

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.

No compatibility data found. Please contribute data for "api.MediaSessionAction" (depth: 1) to the MDN 兼容性数据存储库 .

范例

The following example creates a new media session and assigns action handlers (which don't do anything) to it.

if ('mediaSession' in navigator){
 navigator.mediaSession.metadata = new MediaMetadata({
    title: "Podcast Episode Title",
    artist: "Podcast Host",
    album: "Podcast Name",
    artwork: [{src: "podcast.jpg"}]
  });
 navigator.mediaSession.setActionHandler('play', function() {});
 navigator.mediaSession.setActionHandler('pause', function() {});
 navigator.mediaSession.setActionHandler('seekbackward', function() {});
 navigator.mediaSession.setActionHandler('seekforward', function() {});
 navigator.mediaSession.setActionHandler('previoustrack', function() {});
 navigator.mediaSession.setActionHandler('nexttrack', function() {});
}
			

This example uses appropriate action handlers to allow seeking in either direction through the playing media.

let skipTime = 10; // Time to skip in seconds
navigator.mediaSession.setActionHandler('seekbackward', evt => {
 // User clicked "Seek Backward" media notification icon.
 audio.currentTime = Math.max(audio.currentTime - skipTime, 0);
});
navigator.mediaSession.setActionHandler('seekforward', evt => {
 // User clicked "Seek Forward" media notification icon.
 audio.currentTime = Math.min(audio.currentTime + skipTime,
               audio.duration);
});
			

To remove a media action handler, assign it to null.

navigator.mediaSession.setActionHandler('nexttrack', null);
			

规范

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

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
setActionHandler() Chrome 73 Edge ≤79 Firefox 71 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

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1