Web Audio API
The sinkchange event of the AudioContext interface is fired when the output audio device (and therefore, the AudioContext.sinkId) has changed.
Syntax
Use the event name in EventTarget/addEventListener:
addEventListener("sinkchange", (event) => { })
Alternatively, assign to the event handler property of the interface:
onsinkchange = (event) => { }
Event type
Event.
Event
Examples
A sinkchange event listener can be used to report a change of audio output device. Note that if AudioContext.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 (also check out the source code).
Specifications
Browser compatibility
See also
- Change the destination output device in Web Audio
- AudioContext.setSinkId()
- AudioContext.sinkId
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
