HTMLMediaElement: setSinkId() method
The HTMLMediaElement.setSinkId() method sets the ID of the audio device to use for output and returns a Promise.
This only works when the application is authorized to use the specified device.
Syntax
js
setSinkId(sinkId)
Parameters
sinkId-
The
MediaDeviceInfo.deviceIdof the audio output device.
Return value
Exceptions
NotAllowedErrorDOMException-
Returned if a
speaker-selectionPermissions Policy is used to block use of audio outputs. NotFoundErrorDOMException-
Returned if the
deviceIddoes not match any audio output device. AbortErrorDOMException-
Returned if switching the audio output device to the new audio device failed.
Security requirements
Access to the API is subject to the following constraints:
- The method must be called in a secure context.
- Access may be gated by the
speaker-selectionHTTP Permission Policy.
Examples
js
const devices = await navigator.mediaDevices.enumerateDevices();
const audioDevice = devices.find((device) => device.kind === "audiooutput");
const audio = document.createElement("audio");
await audio.setSinkId(audioDevice.deviceId);
console.log(`Audio is being played on ${audio.sinkId}`);
Specifications
| Specification |
|---|
| Audio Output Devices API # dom-htmlmediaelement-setsinkid |
Browser compatibility
BCD tables only load in the browser