use std::process::{Child, Command, Stdio};
pub struct Player {
process: Option<Child>,
}
impl Player {
pub fn new() -> Self {
Self { process: None }
}
pub fn play(&mut self, url: &str) -> Result<(), Box<dyn std::error::Error>> {
self.stop();
let child = Command::new("mpv")
.arg("--no-video")
.arg("--really-quiet")
.arg(url)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
self.process = Some(child);
Ok(())
}
pub fn stop(&mut self) {
if let Some(ref mut child) = self.process {
let _ = child.kill();
let _ = child.wait();
}
self.process = None;
}
pub fn is_playing(&self) -> bool {
self.process.is_some()
}
}
impl Drop for Player {
fn drop(&mut self) {
self.stop();
}
}