Skip to main content

setMediaSession

Hand one sound to the operating system's media controls. That puts a title and cover art on a phone's lock screen and in the notification shade, and makes the play/pause key on a keyboard and the buttons on a headset control your audio. Reach for it with anything long, like a podcast, an audiobook or a radio stream.

setMediaSession(id: string, info?: MediaSessionInfo): void;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredThe sound or stream the controls should drive.
infoMediaSessionInfo{}Metadata, skip offsets and track callbacks, see below.

MediaSessionInfo​

export interface MediaSessionInfo {
title?: string;
artist?: string;
album?: string;
artwork?: { src: string; sizes?: string; type?: string }[];
seekBackwardOffset?: number;
seekForwardOffset?: number;
onPreviousTrack?: () => void;
onNextTrack?: () => void;
}
PropertyTypeDefaultDescription
titlestring''The main line.
artiststring''Shown underneath.
albumstring''Shown on some platforms.
artwork{ src, sizes?, type? }[][]Cover images. At least one 512x512 image works best everywhere.
seekBackwardOffsetnumber15Seconds the skip-back button jumps.
seekForwardOffsetnumber30Seconds the skip-forward button jumps.
onPreviousTrack() => voidnoneWires the previous-track button. Leave it out and the button is not shown.
onNextTrack() => voidnoneWires the next-track button.

MediaSessionInfo is exported from soundhub as a type.

Returns​

Nothing.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();

await soundHub.loadStream('episode-42', '/audio/episode-42.mp3');
soundHub.play('episode-42');

soundHub.setMediaSession('episode-42', {
title: 'Episode 42: naming things',
artist: 'The Podcast',
album: 'Season 3',
artwork: [
{ src: '/cover-192.png', sizes: '192x192', type: 'image/png' },
{ src: '/cover-512.png', sizes: '512x512', type: 'image/png' },
],
onNextTrack: () => playEpisode(43),
onPreviousTrack: () => playEpisode(41),
});

What it wires up​

ControlWhat happens
Playresume if the sound is paused, otherwise play from the start
Pausepause
Stopstop
Skip backwardseek back by seekBackwardOffset, never before 0
Skip forwardseek forward by seekForwardOffset
Scrubberseek to the dragged position
Previous and nextYour onPreviousTrack and onNextTrack, if you passed them

Good to know​

  • Only one sound owns the media controls. Calling it again, for the same id or another one, takes over the controls. Call it with new metadata when the track changes; you do not need clearMediaSession in between.
  • Metadata is only replaced when you pass at least one of title, artist, album or artwork. Otherwise what was already on the lock screen stays.
  • The playback state and scrubber position follow every event the hub dispatches for this id. Progress events keep the scrubber moving; they are on by default through trackProgress in SoundHubConfig.
  • When the operating system sends its own skip distance, that is used instead of seekBackwardOffset or seekForwardOffset.
  • Media Session is in current Chrome, Edge, Firefox and Safari, on desktop and mobile. Where it is missing, the method logs a line in debug mode and returns. Your audio plays as normal.

See also​