0

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.

cafce25
  • 15,907
  • 4
  • 25
  • 31
KfK
  • 1
  • You have to show how you're using the `new` function, too. For me with `fn main() {let a = new(); a.sink.sleep_until_end();}` it works like a charm, obviously you still have to wait (or otherwise pass some time, do other work, ...) somewhere with the sink and output stream still around. – cafce25 Aug 02 '23 at 18:17
  • Thanks for the quick advise, while that wasnt the problem it lead me to discover that one of my "co-developers" dropped the Player for some testing and didnt remove it. yes i am ashamed – KfK Aug 02 '23 at 18:50
  • That's why it's **strongly recommended** to build a [mre] and provide that in the question, you'll catch about 80%-90% of errors before you even have to ask. Since you found a solution and not dropping the structs is already covered on SO you can just delete this question. – cafce25 Aug 02 '23 at 18:57

0 Answers0