0

I'm working on a web application that requires assigning different audio output devices using setSinkId in Windows 11, but I'm encountering an issue where the audio always defaults to the system speaker regardless of my selection.

Here's a snippet of my code:

btn.onclick = async (evt) => {
  // request device access the bad way,
  // until we get a proper mediaDevices.selectAudioOutput
  (await navigator.mediaDevices.getUserMedia({audio:true}))
      .getTracks().forEach(track => track.stop());
  
  const devices = await navigator.mediaDevices.enumerateDevices();
  const audio_outputs = devices.filter( (device) => device.kind === "audiooutput" );
  
  const sel = document.createElement("select");
  audio_outputs.forEach( (device, i) => sel.append(new Option( device.label || `device ${i}`, device.deviceId )) );
  document.body.append(sel);
  
  btn.textContent = "play audio in selected output";
  btn.onclick = (evt) => {
    const aud = new Audio("https://upload.wikimedia.org/wikipedia/en/d/dc/Strawberry_Fields_Forever_%28Beatles_song_-_sample%29.ogg");
    aud.setSinkId(sel.value);
    aud.play();
  };
}

I've set up a [JSFiddle] https://jsfiddle.net/wvjtg79e/ example to demonstrate the issue.

Steps to Reproduce:

  1. Connect an external speaker to your system.
  2. Open the JSFiddle link and run the code.
  3. Select the system speaker as the output device.

Issue: The audio is played through the connected external speaker instead of the selected system speaker.

Environment: Browser: Microsoft Edge (latest version) OS: Windows 11

Questions:

  1. Has anyone else faced this issue with setSinkId in Edge on Windows 11?
  2. Are there any known workarounds or fixes to ensure the audio plays from the selected speaker?
  3. Could this be a browser-specific issue or a bug in Windows 11?

Your insights and suggestions would be greatly appreciated! This feature is crucial for my application, and any help would be incredibly valuable.

13
  • have you tried in edge in windows 11 with mentioned steps jsfiddle.net/Lhz0bg5e
    – yash bhaje
    Commented Jul 8 at 10:47
  • @JaromandaX playing a audio from selected speaker does not work
    – yash bhaje
    Commented Jul 8 at 11:00
  • Your code doesn't play anything anywhere ... so ... Commented Jul 8 at 11:02
  • It plays audio in External speaker not in selected speaker
    – yash bhaje
    Commented Jul 8 at 11:03
  • the code in the fiddle plays no audio anywhere - I had to add aud.play() when I tested your code, then it worked on the selected output in both chrome and edge Commented Jul 8 at 11:05

0