Rust rodio Sink stops playing when the function scope changes

166 views Asked by At

I am building a simple music player in Rust. For the actual audio playback I have decided to use the rodio crate, specifically the contained Sink. I built a Player struct which contains a Sink, the OutputStream and the OutputStreamHandle. I'm saving these because in another issue that seemed to be the problem. For the sake of simplicity, I am only adding a SineWave to the sink.

pub struct Player {
    streamhandle: (OutputStream, OutputStreamHandle),
    sink: Sink,
    playback: PlayBack,
}

pub fn new() -> Player {
    let (stream, stream_handle) = OutputStream::try_default().unwrap();

    let player = Player {
        sink: Sink::try_new(&stream_handle).unwrap(),
        streamhandle: (stream, stream_handle),
    };

    let source = SineWave::new(200.0)
        .take_duration(Duration::from_secs_f32(2.0))
        .amplify(0.10);
    player.sink.append(source);
    // std::thread::sleep(std::time::Duration::from_millis(1000));

    player
}

When I sleep in the thread after appending to the Sink the sound gets played as long as the thread sleeps. When I don't sleep the thread, the function returns immediately and the program continues outside of it and no sound is played. My first assumption was that dropping the OutputStream/OutputStreamHandle could have been a problem but now as I'm keeping those, the problem still occurs.

0

There are 0 answers