Skip to content

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

Open

Description

@muni-corn

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?

Activity

  1. 9to1url commented on Aug 22, 2020

    @9to1url

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

  2. mdekstrand commented on Jan 26, 2021

    @mdekstrand

    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.

  3. asafg6 commented on Apr 14, 2023

    @asafg6

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions