RustAudio / rodio

Rust audio playback library
Apache License 2.0
1.74k stars 228 forks source link

[Question] Play the same sink between functions #541

Closed imjord closed 7 months ago

imjord commented 8 months ago

Hello im a bit new to rust and had a question how can i keep pausing/playing the same sink between functions? this is my code rn i can play the song and the stop song stops it but it doesnt resume again

lazy_static! {
static ref PLAYBACK_STATE: Mutex<bool> = Mutex::new(false);
}

   async fn play_song() {
    task::spawn_blocking(|| {
    *PLAYBACK_STATE.lock().unwrap() = true;

    let (_stream, stream_handle) = OutputStream::try_default().unwrap();
    let sink = Sink::try_new(&stream_handle).unwrap();
    let file = BufReader::new(File::open("test.mp3").unwrap());
    let source = Decoder::new(file).unwrap();
    sink.append(source);

    while *PLAYBACK_STATE.lock().unwrap() {
        sink.play();
    }
})
.await
.unwrap();
} 

 fn stop_song() {
  println!("here");
  *PLAYBACK_STATE.lock().unwrap() = false;
  let (_stream, stream_handle) = OutputStream::try_default().unwrap();
  let sink = Sink::try_new(&stream_handle).unwrap();
  sink.pause();

}