Skip to main content

clearMediaSession

Take the current sound off the operating system's media controls. It removes the metadata, unhooks every media key and sets the playback state to none, so the lock screen stops showing a player. Call it when playback is finished for good, such as when a listener closes the episode or your component unmounts.

clearMediaSession(): void;

Parameters​

This method takes no parameters.

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

function closePlayer() {
soundHub.stop('episode-42');
soundHub.clearMediaSession();
}

In a React component:

useEffect(() => {
soundHub.setMediaSession('episode-42', { title: 'Episode 42' });
return () => soundHub.clearMediaSession();
}, []);

Good to know​

  • You do not need it between tracks. Calling setMediaSession again with new metadata replaces what is there.
  • unloadSound on a stream that owns the media session calls this for you. Unloading a buffered sound does not, so clear it yourself first.
  • It does not stop or pause the sound. It only takes it off the controls.
  • No event is dispatched. Where the browser has no Media Session support, it does nothing.

See also​