Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The sinkId
read-only property of the AudioContext
interface returns the sink ID of the current output audio device.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The sinkId
read-only property of the AudioContext
interface returns the sink ID of the current output audio device.
This property returns one of the following values, depending on how the sink ID was set:
If a sink ID has not explicitly been set, the default system audio output device will be used, and sinkId
will return an empty string.
If the sink ID is set as a string value (using setSinkId()
, or the sinkId
AudioContext()
constructor option), sinkId
will return that same string value.
AudioSinkInfo
object
If the sink ID is set as an options object (using setSinkId()
, or the sinkId
AudioContext()
constructor option), sinkId
will return an AudioSinkInfo
object reflecting the same values set in the initial options object.
In our SetSinkId test example, we create an audio graph that generates a three-second burst of white noise via an AudioBufferSourceNode
, which we also run through a GainNode
to quiet things down a bit. We also provide the user with a dropdown menu to allow them to change the audio output device.
When the Play button is clicked, we assemble the audio graph and start it playing, and we also log information about the current device to the console based on the value of sinkId
:
type: 'none'
.js
playBtn.addEventListener("click", () => {
const source = audioCtx.createBufferSource();
source.buffer = myArrayBuffer;
source.connect(gain);
gain.connect(audioCtx.destination);
source.start();
if (audioCtx.sinkId === "") {
console.log("Audio playing on default device");
} else if (
typeof audioCtx.sinkId === "object" &&
audioCtx.sinkId.type === "none"
) {
console.log("Audio not playing on any device");
} else {
console.log(`Audio playing on device ${audioCtx.sinkId}`);
}
});
Specification |
---|
Web Audio API # dom-audiocontext-sinkid |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
sinkId |
110 | 110 | No | No | 96 | No | 110 | 110 | No | 74 | No | 21.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/sinkId