I have a page where I need to quickly send some audio, using JavaScript [or jQuery], to the speakers (to be heard in a classroom) and some other audio to a bluetooth device (messages to the teacher). I only want to OUTPUT audio. I DON'T need to get access to any INPUT: not the microphone nor webcam nor anything that has any privacy issues. I just want to play sounds and sync to different devices.
And currently, that all works very well, I can get the list of MediaDevices [input and output] using the enumerateDevices method. Here's a javascript code sample:
async function routeAudioOutput() {
const devices = await navigator.mediaDevices.enumerateDevices();
const audioDevices = devices.filter(device => device.kind === 'audiooutput');
audioDevices.forEach(function(element) {
console.log("ALL DEVICES: ");
console.log(element);
});
}
This retrieves the list of media devices BUT it requires the browser to be configured to allow access to the microphone. Wait: what?!?! Yeah, if you try to run the above code on your browser, you probably won't get the device labels and you won't be able to use the audioDevices to send sound to them, for example, like this:
async function playClassroomSoundAsync() {
var audio = new Audio('notification.wav');
var selectedSinkId = $('#soundDeviceDropdownlist').children("option:selected").val();
await audio.setSinkId(selectedSinkId).then(() => {
console.log("Success, audio output device attached, will play sound on deviceId="+selectedSinkId);
audio.play();
});
};
My obvious guess is because enumerateDevices returns BOTH INPUT and OUTPUT devices (well, I really only want output devices).
So, for now, my users have to setup their browsers to grant access to their microphones!! And some users are, rightly-so, paranoid about that. Furthermore, in some cases, users have to go to "site settings" to grant microphone access because even if the browser is configured to "ask", that popup doesn't always "ask" when users reach my page (depending on browser and other voodoo).
So, here's my question: is there any way to retrieve ONLY OUTPUT DEVICES so that my users DON'T have to grant my page access to their microphones?? Thanks!!
NOTE: This other question: Sound analysis without getUserMedia is very similar but hasn't received a single answer that makes sense in over a year (the one answer is "all over the place"). I think the other question is more broad and contains too many details that are distracting people from the central question: how to select only output devices