Back to Content

AudioContext: sinkchange event

files/en-us/web/api/audiocontext/sinkchange_event/index.md

latest1.6 KB
Original Source

{{APIRef("Web Audio API")}}{{SeeCompatTable}}

The sinkchange event of the {{domxref("AudioContext")}} interface is fired when the output audio device (and therefore, the {{domxref("AudioContext.sinkId")}}) has changed.

Syntax

Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.

js-nolint
addEventListener("sinkchange", (event) => { })

onsinkchange = (event) => { }

Event type

{{domxref("Event")}}.

{{InheritanceDiagram("Event")}}

Examples

A sinkchange event listener can be used to report a change of audio output device. Note that if {{domxref("AudioContext.sinkId", "sinkId")}} contains an {{domxref("AudioSinkInfo")}} object, it indicates that the audio has been changed to not play on any output device.

js
audioCtx.addEventListener("sinkchange", () => {
  if (typeof audioCtx.sinkId === "object" && audioCtx.sinkId.type === "none") {
    console.log("Audio changed to not play on any device");
  } else {
    console.log(`Audio output device changed to ${audioCtx.sinkId}`);
  }
});

See our SetSinkId test example for working code (also check out the source code).

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also