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.


navigator.mediaSession.setActionHandler(type, callback)


A DOMString representing an action type to listen for. It will be one of play, pausestop, seekbackward, seekforwardseektoskipad,previoustrack, or nexttrack. Further details on the action types can be found below under Media session actions.
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



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.


This 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: 'Unforgettable',
    artist: 'Nat King Cole',
    album: 'The Ultimate Collection (Remastered)',
    artwork: [
      { src: '',   sizes: '96x96',   type: 'image/png' },
      { src: '', sizes: '128x128', type: 'image/png' },
      { src: '', sizes: '192x192', type: 'image/png' },
      { src: '', sizes: '256x256', type: 'image/png' },
      { src: '', sizes: '384x384', type: 'image/png' },
      { src: '', sizes: '512x512', type: 'image/png' },

  navigator.mediaSession.setActionHandler('play', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('pause', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('stop', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('seekbackward', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('seekforward', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('seekto', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('previoustrack', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('nexttrack', function() { /* Code excerpted. */ });
  navigator.mediaSession.setActionHandler('skipad', function() { /* Code excerpted. */ });

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 function() {
      // play our audio
      // set playback state
      navigator.mediaSession.playbackState = "playing";
      // update our status element
      updateStatus(allMeta[index], 'Action: play  |  Track is playing...')
    () => {
      // pause out audio
      // 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.

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,  

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

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 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,
    case "seekbackward":
      audio.currentTime = Math.max(audio.currentTime - skipTime, 0);

Here, the handleSeek() function handles both seekbackward and seekforward actions.


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

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Chrome Full support 73Edge Full support ≤79Firefox Full support 71
Full support 71
Disabled From version 71: this feature is behind the preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support YesSafari No support NoWebView Android No support NoChrome Android Full support 57Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android Full support 7.0


Full support  
Full support
No support  
No support
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.