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.
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
stop
Halts playback entirely.
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);
});
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. |
草案 | 初始定义。 |
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. |
草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
用户必须明确启用此特征。
MediaSession
setActionHandler()
setPositionState()