goldfire / howler.js

Javascript audio library for the modern web.
https://howlerjs.com
MIT License
23.74k stars 2.22k forks source link

Implement Speaker Device Selection (setSinkId) #1547

Open mesutyigit opened 2 years ago

mesutyigit commented 2 years ago

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId

your API doesn't provide "setSinkId".

It would be great if you can change the speaker device programmatically.

navigator.mediaDevices.enumerateDevices() result may include multiple "audiooutput" device.

Nawarius commented 2 years ago

I join. Howler is a cool audio library. I use it for mobile sound. I would like to use it for the desktop, but due to the fact that there is no support for setSinkId, I have to use something else

adamthewan commented 1 year ago

@Nawarius , what did you end up using?

Nawarius commented 1 year ago

@adamthewan, Nothing. The task of changing audio outputs fell off and I stopped these experiments

alexfr26 commented 6 months ago

I've finally found a solution for using Howler.js with the setSinkId method. This approach works exclusively with the html5. Hence, it must be explicitly enabled in the Howl instance:


const sound = new Howl({
  src: [soundPath],
  html5: true, // Ensure html5 mode is enabled for setSinkId to work
  onplay: () => {
    const audioElement = sound._sounds[0]._node; // Access the underlying HTMLAudioElement

    if (typeof audioElement.setSinkId === 'function') {
      audioElement
        .setSinkId('sink_device_id') 
        .then(() => {
          console.log('Audio output successfully redirected.'); 
        })
        .catch((error) => {
          console.error('Failed to redirect audio output:', error);
        });
    }
  },
});