Code

MDN Web APIs

A

Audio Session API

Experimental

Provides a mechanism for web applications to express the nature of their audio output so the platform can manage how it interacts with other audio on the device — ducking, pausing, or routing through the appropriate hardware.

MDN Reference

Key Interface
  • Navigator.audioSession — Returns the AudioSession object for the current document.
  • AudioSession.type — Sets/gets the session type. Defaults to "auto".
Session Types
TypeUse case
"auto"Default. Browser infers the best type automatically.
"playback"Music or video — should not mix with other playback.
"transient"Short sounds (notifications) — plays on top of other audio.
"transient-solo"Voice prompts — pauses all other audio while playing.
"ambient"Background audio — can mix freely with other audio.
"play-and-record"Video conferencing — simultaneous playback and capture.
Example — Video conferencing session

Set the session type to "play-and-record" so the platform can route audio appropriately (e.g. earpiece on mobile) and prevent other apps from interrupting the call.

// Declare play-and-record intent before starting media streams
navigator.audioSession.type = 'play-and-record';

// Start playing remote participant audio/video
remoteVideo.srcObject = remoteMediaStream;
remoteVideo.play();

// Capture local audio and video
navigator.mediaDevices
  .getUserMedia({ audio: true, video: true })
  .then((stream) => {
    localVideo.srcObject = stream;
  });
Example — Music playback

Declare a "playback" session so the platform treats it like a media player — allowing hardware media keys to control it and preventing it from mixing with other playback audio.

navigator.audioSession.type = 'playback';

const audio = new Audio('https://example.com/track.mp3');
audio.play();

// React to platform-level interruptions (e.g. incoming call)
navigator.audioSession.addEventListener('statechange', () => {
  if (navigator.audioSession.state === 'interrupted') {
    audio.pause();
  } else if (navigator.audioSession.state === 'active') {
    audio.play();
  }
});