th-ch / youtube-music

YouTube Music Desktop App bundled with custom plugins (and built-in ad blocker / downloader)
https://th-ch.github.io/youtube-music/
MIT License
7.78k stars 470 forks source link

[Feature Request] Select audio output device #164

Open churchymayer opened 3 years ago

churchymayer commented 3 years ago

It would be nice if I could set a custom audio output device

Araxeus commented 3 years ago

Its usually the operating system's job to do that, for example:

Windows 10 lets you define input/output devices for each application separately via “App Volume And Device Preferences” can be opened with command ms-settings:apps-volume (You can set that as shortcut target)

See some random tutorial And random screenshot of that feature:

fedemox commented 2 years ago

Actually it's not so usual... Microsoft added this functionality to Windows Media Player with Windows 7 release and Windows 8.1 audio settings still can't do that. Appunti01

There's an extension for Chrome called AudioXout addressing this issue: it doesn't remember device choice (it should, but doesn't work) and requires only microphone permissions; maybe it could be ported as a plugin for this app. Appunti02

Araxeus commented 2 years ago

If anyone want to make a plugin for this, should probably use HTMLMediaElement.setSinkId()

could use inspiration from https://github.com/rain-fighters/AudioPick

Azureit commented 2 years ago

I needed this feature, and implement the simplest dumb plugin with hardcoded deviceId. For anyone that need this, just create a new directory named "pref-audio-out-device" at "plugins" inside create a file named "front.js" with this content:

module.exports = () => {
    document.addEventListener('apiLoaded', apiEvent => {
        changeToPrefAudioOutDevice();
        navigator.mediaDevices.ondevicechange = (e) => {
            changeToPrefAudioOutDevice();
        };
    }, { once: true, passive: true })
};

function changeToPrefAudioOutDevice() {
    const prefAudioOutDeviceId = '74b147ea818349756def2d6dae3a5422f8be7f0ef7b91749f61cfbe7a83730fe'; //TODO load from plugin options

    navigator.mediaDevices.enumerateDevices().then((devices) => {
        devices.forEach((device) => {
            console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`);
            if (device.kind === 'audiooutput' && // device type is audio output
                device.deviceId === prefAudioOutDeviceId && // preferred audio out device is connected
                document.querySelector('.video-stream,.html5-main-video').SinkId !== prefAudioOutDeviceId) { // preferred audio out device is not already set
                    document.querySelector('.video-stream,.html5-main-video').setSinkId(device.deviceId); // set preferred audio out device
            }
        });
    }).catch((err) => {
        console.error(`${err.name}: ${err.message}`);
    });
}

'74b147ea818349756def2d6dae3a5422f8be7f0ef7b91749f61cfbe7a83730fe' is the hardcoded deviceId of My preferred audio out device :) See the console log for a list of your devices.

The navigator.mediaDevices.ondevicechange is not working, only change at app start :(

Testing with git version, with kde plasma 5.24.6

akumenon commented 1 year ago

Just want to express my interest in this as well. I tried what Azureit explained but had compilation failures which I don't know how to diagnose/fix. Hoping this will be implemented at some point, since setting it in Windows' sound mixer doesn't persist after rebooting (edit: even restarting the application).

Zo-Bro-23 commented 1 year ago

Just want to express my interest in this as well. I tried what Azureit explained but had compilation failures which I don't know how to diagnose/fix. Hoping this will be implemented at some point, since setting it in Windows' sound mixer doesn't persist after rebooting.

Try going to Window Settings > Audio (maybe under Devices) > App Volume and selecting audio device. The names might be different (I'm not on Windows now so I can't check), but I know that there's an option in Settings that persists even after reboot. I've used it before in both Win 10 and 11.

Zo-Bro-23 commented 1 year ago

But +1 for adding this feature to the YTM app.

stoicpenguin2234 commented 1 year ago

i also tried azureit mentioned and no success hopefully this does get added

Araxeus commented 1 year ago

should be pretty easy to do after those:

  1. https://developer.chrome.com/blog/audiocontext-setsinkid/ (Published on Wednesday, January 11, 2023)
  2. https://github.com/th-ch/youtube-music/pull/951 (Merged on Sunday, January 8, 2023)

Anyone want to give it a shot?