The HTMLMediaElement.setSinkId()
method of the Audio Output Devices API sets the ID of the audio device to use for output and returns a Promise
.
This only works when the application is permitted to use the specified device. For more information see the security requirements below.
A Promise
that resolves to undefined
.
Access to the API is subject to the following constraints:
This example shows how to select an audio output device from the array returned by MediaDevices.enumerateDevices()
, and set it as the sink for audio. Note that the result of enumerateDevices()
only includes devices for which user permission is not required or has already been 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}`);