The MediaDevices
method selectAudioOutput()
prompts the user to select a specific audio output device, for example a speaker or headset. On success, the returned Promise
is resolved with a MediaDeviceInfo
describing the selected device.
The method must be triggered from some UI interaction like a button click (more precisely, it requires transient activation).
Access to audio output devices is gated by the Permissions API. The prompt will not be displayed if the speaker-selection
permission has not been granted.
selectAudioOutput()
selectAudioOutput(options)
A Promise
that receives a MediaDeviceInfo
object when the promise is fulfilled. The object describes the user-selected audio output device.
Here's an example of using selectAudioOutput()
, which you might call within a function that is triggered by a button click. It outputs the selected device IDs and labels (if available) or an error message.
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(function(err) {
console.log(err.name + ": " + err.message);
});
On selection of an output this might produce:
audiooutput: Realtek Digital Output (Realtek(R) Audio) id = 0wE6fURSZ20H0N2NbxqgowQJLWbwo+5ablCVVJwRM3k=