RustAudio / rodio

Rust audio playback library
Apache License 2.0
1.66k stars 214 forks source link

Play sound on system output device, even if the system output device changes #544

Closed sloganking closed 3 months ago

sloganking commented 4 months ago

I want sound to play on whatever the operating system's default audio output device is, and switch to the new default audio device if the system changes which one is default. Below is an image showing where the default audio device is chosen on Windows 10.

image

let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&stream_handle).unwrap();

Currently sounds played on the above sink via

let file = std::fs::File::open(ai_speech_segment.path()).unwrap();
sink.stop();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
//sink.play()
sink.sleep_until_end();

Don't ever switch which audio device they are outputting sound to if the operating system's default audio device changes. This behavior is different than all other applications on my device that I know of.

I could re-create a sink every time I want to play a sound. but that's annoying and doesn't change audio devices until the current sound ends and the next one is played. Is there a better solution, or can we make one?

===

edit: as a side note. I am not sure why sounds play when you append them and you don't need to do sink.play(). Let me know if that is expected behavior!

dvdsk commented 3 months ago

This seems to be an windows only issue in cpal, related issue: https://github.com/RustAudio/cpal/issues/740

Nothing we can do here.