The MediaSession
interface of the Media Session API allows a web page to provide custom behaviors for standard media playback interactions, and to report metadata that can be sent by the user agent to the device or operating system for presentation in standardized user interface elements.
For example, a smartphone might have a standard panel in its lock screen that provides controls for media playback and information display. A browser on the device can use MediaSession
to make browser playback controllable from that standard/global user interface.
Instance properties
-
metadata
-
Returns an instance of MediaMetadata
, which contains rich media metadata for display in a platform UI.
-
playbackState
-
Indicates whether the current media session is playing. Valid values are none
, paused
, or playing
.
Instance methods
-
setActionHandler()
-
Sets an action handler for a media session action, such as play or pause.
-
setPositionState()
-
Sets the current playback position and speed of the media currently being presented.
Examples
Setting up action handlers for a music player
The following example creates a new media session and assigns action handlers to it:
if ("mediaSession" in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: "Unforgettable",
artist: "Nat King Cole",
album: "The Ultimate Collection (Remastered)",
artwork: [
{
src: "https://dummyimage.com/96x96",
sizes: "96x96",
type: "image/png",
},
{
src: "https://dummyimage.com/128x128",
sizes: "128x128",
type: "image/png",
},
{
src: "https://dummyimage.com/192x192",
sizes: "192x192",
type: "image/png",
},
{
src: "https://dummyimage.com/256x256",
sizes: "256x256",
type: "image/png",
},
{
src: "https://dummyimage.com/384x384",
sizes: "384x384",
type: "image/png",
},
{
src: "https://dummyimage.com/512x512",
sizes: "512x512",
type: "image/png",
},
],
});
navigator.mediaSession.setActionHandler("play", () => {
});
navigator.mediaSession.setActionHandler("pause", () => {
});
navigator.mediaSession.setActionHandler("stop", () => {
});
navigator.mediaSession.setActionHandler("seekbackward", () => {
});
navigator.mediaSession.setActionHandler("seekforward", () => {
});
navigator.mediaSession.setActionHandler("seekto", () => {
});
navigator.mediaSession.setActionHandler("previoustrack", () => {
});
navigator.mediaSession.setActionHandler("nexttrack", () => {
});
navigator.mediaSession.setActionHandler("skipad", () => {
});
navigator.mediaSession.setActionHandler("togglecamera", () => {
});
navigator.mediaSession.setActionHandler("togglemicrophone", () => {
});
navigator.mediaSession.setActionHandler("hangup", () => {
});
}
The following example sets up two functions for playing and pausing, then uses them as callbacks with the relevant action handlers.
const actionHandlers = [
[
"play",
async () => {
await audioEl.play();
navigator.mediaSession.playbackState = "playing";
updateStatus(allMeta[index], "Action: play | Track is playing…");
},
],
[
"pause",
() => {
audioEl.pause();
navigator.mediaSession.playbackState = "paused";
updateStatus(allMeta[index], "Action: pause | Track has been paused…");
},
],
];
for (const [action, handler] of actionHandlers) {
try {
navigator.mediaSession.setActionHandler(action, handler);
} catch (error) {
console.log(`The media session action "${action}" is not supported yet.`);
}
}
Using action handlers to control a slide presentation
The "previousslide"
and "nextslide"
action handlers can be used to handle moving forward and backward through a slide presentation, for example when the user puts their presentation into a Picture-in-Picture window, and presses the browser-supplied controls for navigating through slides.
try {
navigator.mediaSession.setActionHandler("previousslide", () => {
log('> User clicked "Previous Slide" icon.');
if (slideNumber > 1) slideNumber--;
updateSlide();
});
} catch (error) {
log('Warning! The "previousslide" media session action is not supported.');
}
try {
navigator.mediaSession.setActionHandler("nextslide", () => {
log('> User clicked "Next Slide" icon.');
slideNumber++;
updateSlide();
});
} catch (error) {
log('Warning! The "nextslide" media session action is not supported.');
}
See Presenting Slides / Media Session Sample for a working example.
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 |
MediaSession |
73 |
79 |
82 |
No |
60 |
15 |
No |
57 |
82Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
43 |
15 |
7.0 |
metadata |
73 |
79 |
82 |
No |
60 |
15 |
No |
57 |
82Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
43 |
15 |
7.0 |
playbackState |
73 |
79 |
82 |
No |
60 |
15 |
No |
57 |
82Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
43 |
15 |
7.0 |
setActionHandler |
73 |
79 |
82Firefox does not support the togglemicrophone , togglecamera , and hangup action types.
|
No |
60 |
15Safari does not support for the togglemicrophone , togglecamera , and hangup action types.
|
No |
57 |
82Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
43 |
15Safari does not support for the togglemicrophone , togglecamera , and hangup action types.
|
7.0 |
setCameraActive |
93 |
93 |
No |
No |
79 |
No |
No |
93 |
No |
66 |
No |
17.0 |
setMicrophoneActive |
93 |
93 |
No |
No |
79 |
No |
No |
93 |
No |
66 |
No |
17.0 |
setPositionState |
81 |
81 |
82 |
No |
68 |
15 |
No |
57 |
82Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
|
43 |
15 |
7.0 |