MediaSession.setActionHandler()

The setActionHandler() property of the 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.

Syntax

navigator.mediaSession.setActionHandler(type, callback)

Parameters

type
A DOMString representing an action type to listen for. It will be one of play, pause, seekbackward, seekforward, seekto, skipad,previoustrack, or 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.

Return value

undefined.

Description

To remove a previously-established action handler, call setActionHandler() again, specifying null as the 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.

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.
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 user agent, or may be disabled due to subscription level or other circumstances.
stop
Halts playback entirely.

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', 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 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.

Specifications

Specification Status Comment
Media Session Standard
The definition of 'Media Session action types' in that specification.
Draft Initial definition.

Browser compatibility

No compatibility data found. Please contribute data for "api.MediaSessionAction" (depth: 1) to the MDN compatibility data repository.

Examples

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

Specifications

Specification Status Comment
Media Session Standard
The definition of 'MediaSession.setActionHandler()' in that specification.
Draft Initial definition.

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
setActionHandler()
Experimental
Chrome Full support 73Edge Full support ≤79Firefox Full support 71
Disabled
Full support 71
Disabled
Disabled From version 71: this feature is behind the dom.media.mediasession.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support YesSafari ? WebView Android No support NoChrome Android Full support 57Firefox Android No support NoOpera Android No support NoSafari iOS ? Samsung Internet Android Full support 7.0

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
User must explicitly enable this feature.
User must explicitly enable this feature.