MediaSession: setActionHandler() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The setActionHandler() method of the MediaSession interface sets a 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

js
setActionHandler(type, callback)

Parameters

type

A string representing an action type to listen for. It will be one of the following:

hangup

End a call.

nextslide

Moves to the next slide, when presenting a slide deck.

nexttrack

Advances playback to the next track.

pause

Pauses playback of the media.

play

Begins (or resumes) playback of the media.

previousslide

Moves to the previous slide, when presenting a slide deck.

previoustrack

Moves back to the previous track.

seekbackward

Seeks backward through the media from the current position. The seekOffset property passed to the callback specifies the amount of time to seek backward.

seekforward

Seeks forward from the current position through the media. The seekOffset property passed to the callback 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 seekTime property passed to the callback. If you intend to perform multiple seekto operations in rapid succession, you can also specify the fastSeek property passed to the callback 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

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.

togglecamera

Turn the user's active camera on or off.

togglemicrophone

Mute or unmute the user's microphone.

togglescreenshare

Turn the user's active screenshare on or off.

callback

A function to call when the specified action type is invoked. The callback should not return a value. The callback receives a dictionary containing the following properties:

action

A string representing the action type. This property allows a single callback to handle multiple action types.

fastSeek Optional

A 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 Optional

If the action is either 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 Optional

If the 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.

Return value

None (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 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.

Examples

Setting up action handlers for a music player

This example creates a new media session and assigns action handlers (which don't do anything) to it.

js
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", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("pause", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("stop", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("seekbackward", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("seekforward", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("seekto", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("previoustrack", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("nexttrack", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("skipad", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("togglecamera", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("togglemicrophone", () => {
    /* Code excerpted. */
  });
  navigator.mediaSession.setActionHandler("hangup", () => {
    /* Code excerpted. */
  });
}

The following example sets up two functions for playing and pausing, then uses them as callbacks with the relevant action handlers.

js
const actionHandlers = [
  // play
  [
    "play",
    async () => {
      // play our audio
      await audioEl.play();
      // set playback state
      navigator.mediaSession.playbackState = "playing";
      // update our status element
      updateStatus(allMeta[index], "Action: play  |  Track is playing…");
    },
  ],
  [
    "pause",
    () => {
      // pause out audio
      audioEl.pause();
      // set playback state
      navigator.mediaSession.playbackState = "paused";
      // update our status element
      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.`);
  }
}

This example uses appropriate action handlers to allow seeking in either direction through the playing media.

js
navigator.mediaSession.setActionHandler("seekbackward", (evt) => {
  // User clicked "Seek Backward" media notification icon.
  let skipTime = evt.seekOffset || 10; // Time to skip in seconds
  audio.currentTime = Math.max(audio.currentTime - skipTime, 0);
});

navigator.mediaSession.setActionHandler("seekforward", (evt) => {
  // User clicked "Seek Forward" media notification icon.
  let skipTime = evt.seekOffset || 10; // Time to skip in seconds
  audio.currentTime = Math.min(audio.currentTime + skipTime, audio.duration);
});

To remove a media action handler, assign it to null.

js
navigator.mediaSession.setActionHandler("nexttrack", null);

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 action property:

js
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 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.

js
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

Specification
Media Session
# dom-mediasession-setactionhandler

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
setActionHandler()
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Partial support
footnote Firefox exposes the API, but does not provide a corresponding user-facing media control interface.
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"hangup" type
Chrome – Full support
Chrome 93 (Release date: 2021-08-31)
footnote Full support
Edge – Full support
Edge 93 (Release date: 2021-09-02)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 79 (Release date: 2021-09-14)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 93 (Release date: 2021-08-31)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 66 (Release date: 2021-12-15)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 17 (Release date: 2022-05-04)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
"nextslide" type
Chrome – Full support
Chrome 111 (Release date: 2023-03-07)
footnote Full support
Edge – Full support
Edge 111 (Release date: 2023-03-13)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 97 (Release date: 2023-03-22)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 111 (Release date: 2023-03-07)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 75 (Release date: 2023-05-17)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 22 (Release date: 2023-07-14)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
"nexttrack" type
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"pause" type
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"play" type
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"previousslide" type
Chrome – Full support
Chrome 111 (Release date: 2023-03-07)
footnote Full support
Edge – Full support
Edge 111 (Release date: 2023-03-13)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 97 (Release date: 2023-03-22)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 111 (Release date: 2023-03-07)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 75 (Release date: 2023-05-17)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 22 (Release date: 2023-07-14)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote No support
"previoustrack" type
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"seekbackward" type
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"seekforward" type
Chrome – Full support
Chrome 73 (Release date: 2019-03-12)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"seekto" type
Chrome – Full support
Chrome 78 (Release date: 2019-10-22)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 65 (Release date: 2019-11-13)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 78 (Release date: 2019-10-22)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 56 (Release date: 2020-02-06)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 12 (Release date: 2020-06-19)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"skipad" type
Chrome – Full support
Chrome 128 (Release date: 2024-08-20)
footnote Full support
Edge – Full support
Edge 128 (Release date: 2024-08-22)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 114 (Release date: 2024-09-25)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 128 (Release date: 2024-08-20)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 85 (Release date: 2024-10-29)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 28 (Release date: 2025-04-02)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"stop" type
Chrome – Full support
Chrome 77 (Release date: 2019-09-10)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 82 (Release date: 2020-10-20)
footnote Full support
Opera – Full support
Opera 64 (Release date: 2019-10-07)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 77 (Release date: 2019-09-10)
footnote Full support
Firefox for Android – Full support
Firefox for Android 82 (Release date: 2020-10-20)
footnote Full support
Opera Android – Full support
Opera Android 55 (Release date: 2019-12-03)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 12 (Release date: 2020-06-19)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
"togglecamera" type
Chrome – Full support
Chrome 93 (Release date: 2021-08-31)
footnote Full support
Edge – Full support
Edge 93 (Release date: 2021-09-02)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 79 (Release date: 2021-09-14)
footnote Full support
Safari – Full support
Safari 18.4 (Release date: 2025-03-31)
footnote Full support
Chrome Android – Full support
Chrome Android 93 (Release date: 2021-08-31)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 66 (Release date: 2021-12-15)
footnote Full support
Safari on iOS – Full support
Safari on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Samsung Internet – Full support
Samsung Internet 17 (Release date: 2022-05-04)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
"togglemicrophone" type
Chrome – Full support
Chrome 93 (Release date: 2021-08-31)
footnote Full support
Edge – Full support
Edge 93 (Release date: 2021-09-02)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 79 (Release date: 2021-09-14)
footnote Full support
Safari – Full support
Safari 18.4 (Release date: 2025-03-31)
footnote Full support
Chrome Android – Full support
Chrome Android 93 (Release date: 2021-08-31)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 66 (Release date: 2021-12-15)
footnote Full support
Safari on iOS – Full support
Safari on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Samsung Internet – Full support
Samsung Internet 17 (Release date: 2022-05-04)
footnote Full support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – Full support
WebView on iOS 18.4 (Release date: 2025-03-31)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support