AudioContext: sinkId property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The sinkId read-only property of the AudioContext interface returns the sink ID of the current output audio device.

Value

This property returns one of the following values, depending on how the sink ID was set:

An empty string

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.

A 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.

An 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.

Examples

In our SetSinkId test example (check out the source code), 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:

  • An empty string means the default device is still being used.
  • If the value is an object, the audio will not be playing on any device because we set an options object containing type: 'none'.
  • Otherwise the value will be a sink ID string, so we log that.
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}`);
  }
});

Specifications

Specification
Web Audio API
# dom-audiocontext-sinkid

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
sinkId
Experimental
Chrome – Full support
Chrome 110 (Release date: 2023-02-07)
footnote Full support
Edge – Full support
Edge 110 (Release date: 2023-02-09)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 96 (Release date: 2023-02-22)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 110 (Release date: 2023-02-07)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 74 (Release date: 2023-03-13)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Full support
WebView Android – Full support
WebView Android 110 (Release date: 2023-02-07)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

See also