Media Session API'
s
MediaSessionActionDetails
dictionary is the type used by the sole input parameter into the callback which is executed when a
media session action
出现。
It specifies the type of action which needs to be performed as well as the data needed to perform the action.
action
MediaSessionAction
enumerated type, indicating which type of action needs to be performed. See
Media action types
below for possible values.
fastSeek
可选
seekto
action may
optionally
include this property, which is a Boolean value indicating whether or not to perform a "fast" seek. A "fast" seek is a seek being performed in a rapid sequence, such as when fast-forwarding or reversing through the media, rapidly skipping through it. This property can be used to indicate that you should use the shortest possible method to seek the media.
fastSeek
is not included on the final action in the seek sequence in this situation.
seekOffset
可选
action
是
seekforward
or
seekbackward
and this property is present, it is a floating point value which indicates the number of seconds to move the play position forward or backward. If this property isn't present, those actions should choose a reasonable default distance to skip forward or backward (such as 7 or 10 seconds).
seekTime
可选
action
is
seekto
, this property must be present and must be a floating-point value indicating the absolute time within the media to move the playback position to, where 0 indicates the beginning of the media. This property is not present for other action types.
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
MediaSessionActionDetails
property
seekOffset
specifies the amount of time to seek backward.
seekforward
MediaSessionActionDetails
property
seekOffset
specifies the amount of time to seek forward.
seekto
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
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', 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 兼容性数据存储库 .
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
对象的
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.
In this example, instead of seeking a specified distance, the
seekforward
and
seekbackward
handlers use the time specified in the
MediaSessionActionDetails
对象的
seekOffset
特性。
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;
}
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
Media Session Standard
The definition of 'MediaSessionActionDetails' in that specification. |
草案 | 初始定义。 |
No compatibility data found. Please contribute data for "api.MediaSessionActionDetails" (depth: 1) to the MDN 兼容性数据存储库 .
MediaSessionActionDetails