RustAudio / rodio

Rust audio playback library
Apache License 2.0
1.8k stars 235 forks source link

Is there a way to stop the background thread after playing a sound? #299

Open muni-corn opened 4 years ago

muni-corn commented 4 years ago

After using play_once or play_raw on a sound and audio device (both local variables), the playback stream still shows up in pavucontrol:

pavucontrol

The thread also still exists when I inspect htop, and the thread is using around 3% CPU:

20200709-231723

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?

9to1url commented 4 years ago

Any update on this issue? I also want to know how to pause or stop a playing music running on another thread. thanks

mdekstrand commented 3 years ago

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.

asafg6 commented 1 year ago

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