rodio
rodio copied to clipboard
Is there a way to stop the background thread after playing a sound?
After using play_once
or play_raw
on a sound and audio device (both local variables), the playback stream still shows up in pavucontrol
:
The thread also still exists when I inspect htop, and the thread is using around 3% CPU:
Code I'm using currently:
fn play_new_hour_sound() {
if let Some(audio_device) = rodio::default_output_device() {
let cursor = Cursor::new(include_bytes!("../new_hour.wav").as_ref());
if let Ok(sink) = rodio::play_once(&audio_device, cursor) {
sink.sleep_until_end()
};
}
}
I would expect the thread to stop after audio_device
and sink
are dropped (or audio_device
and source
if using play_raw
), but the thread still runs. Is there a way to stop rodio's background thread after playing a sound?
Any update on this issue? I also want to know how to pause or stop a playing music running on another thread. thanks
I have been investigating high CPU usage on low-power devices (e.g. Raspberry Pi Zero W), over in librespot-org/librespot#567; I suspect it may be this problem (or a related problem). At idle, Rodio consumes 12-13% of a 1GHz ARMv6 core.
I saw 100% cpu usage on my machine when it was loaded but idle. Not the best solution but works for me in the mean time:
pub fn sleep_and_unload(&mut self) {
if let Some(sink) = &self.sink {
sink.sleep_until_end();
self.sink = None;
self.stream_handle = None;
self._stream = None;
}
}
Drops CPU back to 0