The MediaDevices.selectAudioOutput()
method of the Audio Output Devices API prompts the user to select an audio output device, such as a speaker or headset. If the user selects a device, the method grants user permission to use the selected device as an audio output sink.
Following selection, if the device is available it can be enumerated using MediaDevices.enumerateDevices()
and set as the audio output sink using HTMLMediaElement.setSinkId()
.
On success, the returned Promise
is resolved with a MediaDeviceInfo
describing the selected device.
selectAudioOutput()
selectAudioOutput(options)
A Promise
that is fulfilled with a MediaDeviceInfo
object that describes the audio output device selected by the user.
Access to the API is subject to the following constraints:
The permission status can be queried using the Permissions API method navigator.permissions.query()
, passing a permission descriptor with the speaker-selection
permission.
Here's an example of using selectAudioOutput()
, within a function that is triggered by a button click. It outputs the selected device IDs and labels (if available) or an error message.
document.querySelector("#myButton").addEventListener("click", () => {
if (!navigator.mediaDevices.selectAudioOutput) {
console.log("selectAudioOutput() not supported.");
return;
}
navigator.mediaDevices
.selectAudioOutput()
.then((device) => {
console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`);
})
.catch((err) => {
console.error(`${err.name}: ${err.message}`);
});
});
On selection of an output this might produce:
audiooutput: Realtek Digital Output (Realtek(R) Audio) id = 0wE6fURSZ20H0N2NbxqgowQJLWbwo+5ablCVVJwRM3k=