Media Session action types
The MediaSessionAction
enumerated type describes an action that the user may perform in a media session, such as playing, pausing, or seeking.
You pass one of these values into the MediaSession
interface's setActionHandler()
method to indicate the action for which the handler will be called.
Syntax
A media session action's type is specified using a string from the MediaSessionAction
enumerated type.
Values
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
propertyseekOffset
specifies the amount of time to seek backward. seekforward
-
Seeks forward from the current position through the media. The
MediaSessionActionDetails
propertyseekOffset
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
propertyseekTime
. If you intend to perform multipleseekto
operations in rapid succession, you can also specify theMediaSessionActionDetails
propertyfastSeek
property with a value oftrue
. 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.
Description
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.
Examples
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', 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.
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
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.
Using data provided by MediaSessionActionDetails
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; } });
Specifications
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
MediaSessionAction |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
nexttrack |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
pause |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
play |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
previoustrack |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
seekbackward |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
seekforward |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
seekto |
77 |
79 |
82
71
|
No |
No |
No |
No |
77 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
skipad |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
stop |
73 |
79 |
82
71
|
No |
No |
No |
No |
57 |
82
Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
No |
No |
7.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaSessionAction