RustAudio / rodio

Rust audio playback library
Apache License 2.0
1.73k stars 223 forks source link

No sound when unplugging and plugging audio device back in #271

Open Raz-Hemo opened 4 years ago

Raz-Hemo commented 4 years ago

Say I unplug my headphones and plug them in again. What happens to me right now is that even after the replug, I have no audio output (from play_raw()). I tried calling default_output_device() again but the problem persists - the function returns Some(Device), but calling play_raw() on it plays no sound. Restarting my app works, though.

forbjok commented 3 years ago

Same issue. As far as I can tell the issue is that OutputStream ties itself to a specific physical device at the time of creation. This means that if the specific device it is tied to gets disconnected, it will stop working, and also that if you change the default output device at a later time, it will ignore the change and keep playing audio on the old device.

Rodrigodd commented 2 years ago

I was looking at Rodio for reference in how it handle device disconnection (to use on my code), but it does not handle it. The relevant code:

https://github.com/RustAudio/rodio/blob/0356d810c9e7309c12f6e4c20f0add64420ecf57/src/stream.rs#L198-L230

The tricky part is that the previous Stream needs to be dropped and a new one created, but Stream is not Send so there is no way to do that in the error_callback (the callback must be Send). In my code I will probably need to handle the cpal state in another thread, and handle the error there by messaging, but this would not work for WebAssembly...

Edit: looking at kira, it does pretty much exactly what I describe, and doesn't handle device disconnection in WebAssembly (it may not even be necessary, on second thought).