The sinkchange event of the AudioContext interface is fired when the output audio device (and therefore, the AudioContext.sinkId) has changed.
Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener("sinkchange", (event) => {});
onsinkchange = (event) => {};
A sinkchange event listener can be used to report a change of audio output device. Note that if sinkId contains an AudioSinkInfo object, it indicates that the audio has been changed to not play on any output device.
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.