fix: move audio playback into another thread
This commit is contained in:
parent
0e03745f0b
commit
2f971c96a1
26
src/alert.rs
26
src/alert.rs
|
@ -1,5 +1,4 @@
|
||||||
use rodio::{Decoder, Source, OutputStream};
|
use rodio::{Decoder, OutputStream, Sink, Source};
|
||||||
use std::error::Error;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
@ -18,12 +17,23 @@ impl Alert {
|
||||||
self.path = path;
|
self.path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn play(&self) -> Result<(), Box<dyn Error>> {
|
pub fn play(&self) {
|
||||||
let (_, handle) = OutputStream::try_default()?;
|
let file = File::open(&self.path).unwrap();
|
||||||
let file = File::open(&self.path)?;
|
|
||||||
let source = Decoder::new(BufReader::new(file))?;
|
|
||||||
|
|
||||||
handle.play_raw(source.convert_samples()).unwrap(); // use ?, blocked: https://github.com/RustAudio/rodio/pull/329
|
std::thread::spawn(move || {
|
||||||
Ok(())
|
let (_stream, handle) = OutputStream::try_default().unwrap();
|
||||||
|
let source = Decoder::new(BufReader::new(file)).unwrap();
|
||||||
|
let sink = Sink::try_new(&handle).unwrap();
|
||||||
|
|
||||||
|
sink.append(source);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if sink.len() == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Exited Playback Thread.");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,7 +132,7 @@ impl Pomodoro {
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(alert) = maybe_alert {
|
if let Some(alert) = maybe_alert {
|
||||||
let _ = alert.play();
|
alert.play()
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = self.notify();
|
let _ = self.notify();
|
||||||
|
|
Loading…
Reference in New Issue