-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
71 lines (54 loc) · 2.95 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---
title: "AudioContext: sinkId property"
short-title: sinkId
slug: Web/API/AudioContext/sinkId
page-type: web-api-instance-property
status:
- experimental
browser-compat: api.AudioContext.sinkId
---
{{APIRef("Web Audio API")}}{{SeeCompatTable}}{{SecureContext_Header}}
The **`sinkId`** read-only property of the
{{domxref("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 {{domxref("AudioContext.setSinkId", "setSinkId()")}}, or the `sinkId` {{domxref("AudioContext.AudioContext", "AudioContext()")}} constructor option), `sinkId` will return that same string value.
- An {{domxref("AudioSinkInfo")}} object
- : If the sink ID is set as an options object (using {{domxref("AudioContext.setSinkId", "setSinkId()")}}, or the `sinkId` {{domxref("AudioContext.AudioContext", "AudioContext()")}} constructor option), `sinkId` will return an {{domxref("AudioSinkInfo")}} object reflecting the same values set in the initial options object.
## Examples
In our [SetSinkId test example](https://set-sink-id.glitch.me/), we create an audio graph that generates a three-second burst of white noise via an {{domxref("AudioBufferSourceNode")}}, which we also run through a {{domxref("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
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [SetSinkId test example](https://set-sink-id.glitch.me/)
- [Change the destination output device in Web Audio](https://developer.chrome.com/blog/audiocontext-setsinkid/)
- {{domxref("AudioContext.setSinkId()")}}
- {{domxref("AudioContext/sinkchange_event", "sinkchange")}}