HTMLMediaElement: setSinkId() method
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
ThesetSinkId() method of theHTMLMediaElement interface sets the ID of the audio device to use for output and returns aPromise.
This only works when the application is permitted to use the specified device.For more information see thesecurity requirements below.
In this article
Syntax
setSinkId(sinkId)Parameters
sinkIdThe
MediaDeviceInfo.deviceIdof the audio output device.
Return value
Exceptions
NotAllowedErrorDOMExceptionReturned if a
speaker-selectionPermissions Policy is used to block use of audio outputs.NotFoundErrorDOMExceptionReturned if the
deviceIddoes not match any audio output device.AbortErrorDOMExceptionReturned 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 asecure context.
- Access may be gated by the
speaker-selectionHTTPPermission Policy. - User permission is required to access a non-default device.The user grants permission by selecting the device associated with the ID in the prompt displayed by
MediaDevices.selectAudioOutput().
Examples
This example shows how to select an audio output device from the array returned byMediaDevices.enumerateDevices(), and set it as the sink for audio.Note that the result ofenumerateDevices() only includes devices for which user permission is not required or has already been granted.
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 output on ${audio.sinkId}`);Specifications
| Specification |
|---|
| Audio Output Devices API> # dom-htmlmediaelement-setsinkid> |