w3c/mediasession

Add new callActionHandler method

Opened this issue · 4 comments

Make it possible for JavaScript to be able to trigger a MediaSession action handler

the IDL would become:

[Exposed=Window]
interface MediaSession {
  attribute MediaMetadata? metadata;

  attribute MediaSessionPlaybackState playbackState;

  undefined setActionHandler(MediaSessionAction action, MediaSessionActionHandler? handler);

  Promise<undefined> callActionHandler(MediaSessionActionDetails actionDetails);

  undefined setPositionState(optional MediaPositionState state = {});

  undefined setMicrophoneActive(boolean active);

  undefined setCameraActive(boolean active);
};

callActionHandler would synchronously trigger a call to either the associated MediaSession MediaSessionActionHandler and return a resolved promise; or if a custom handler doesn't exist would call the default User Agent handler that would have normally been called for such action.
The promise returned would be resolved if no error occurred, or be rejected if an invalid state was encountered (e.g no element, media element etc defined).

Hello Jean-Yves,
May you explain why web developers would need this?

In this case below for instance, it seems like you suggest developer would call navigator.mediaSession. callActionHandler('play') where they could currently simply call onMediaSessionPlay(). I'm sure there's more and would love to understand.

navigator.mediaSession.setActionHandler('play', onMediaSessionPlay);

async function onMediaSessionPlay() {
  // Resume playback
  await document.querySelector('video').play();
  navigator.mediaSession.playbackState = 'playing';
});

It lets you simplify the code, use anonymous method etc.

For the record, when asked this question by @eric-carlson at https://bugs.webkit.org/show_bug.cgi?id=226077#c6, the answer was a bit different: https://trac.webkit.org/changeset/278222/webkit

Make it possible for JavaScript to be able to trigger a MediaSession action handler so a MediaSession.coordinator polyfill such as one provided by an external JS library will work.

Looking forward to when MediaSession.coordinator will be announced publicly then. Hopefully next week ;)

For the record, when asked this question by @eric-carlson at https://bugs.webkit.org/show_bug.cgi?id=226077#c6, the answer was a bit different: https://trac.webkit.org/changeset/278222/webkit

Make it possible for JavaScript to be able to trigger a MediaSession action handler so a MediaSession.coordinator polyfill such as one provided by an external JS library will work.

Looking forward to when MediaSession.coordinator will be announced publicly then. Hopefully next week ;)

We have posted an explainer with our ideas: https://github.com/WebKit/explainers/tree/main/MediaSessionCoordinator.