diff --git a/mps-player/src/lib.rs b/mps-player/src/lib.rs index 2e0b18f..245b011 100644 --- a/mps-player/src/lib.rs +++ b/mps-player/src/lib.rs @@ -14,6 +14,8 @@ pub(crate) mod uri; pub use controller::MpsController; pub use errors::{PlaybackError, UriError, PlayerError}; pub use player::MpsPlayer; +#[cfg(feature = "mpd")] +pub use player::mpd_connection; //pub use utility::{play_script}; #[cfg(test)] diff --git a/mps-player/src/player.rs b/mps-player/src/player.rs index 5a33f94..2e2b009 100644 --- a/mps-player/src/player.rs +++ b/mps-player/src/player.rs @@ -6,7 +6,7 @@ use rodio::{decoder::Decoder, OutputStream, OutputStreamHandle, Sink}; use m3u8_rs::{MediaPlaylist, MediaSegment}; #[cfg(feature = "mpd")] -use mpd::{Client, Song}; +use mpd::{Client, Song, error}; use super::uri::Uri; @@ -48,6 +48,11 @@ impl<'a, T: MpsTokenReader + 'a> MpsPlayer<'a, T> { Ok(()) } + #[cfg(feature = "mpd")] + pub fn set_mpd(&mut self, client: Client) { + self.mpd_connection = Some(client); + } + pub fn play_all(&mut self) -> Result<(), PlayerError> { while let Some(item) = self.runner.next() { self.sink.sleep_until_end(); @@ -243,6 +248,11 @@ impl<'a, T: MpsTokenReader + 'a> MpsPlayer<'a, T> { } } +#[cfg(feature = "mpd")] +pub fn mpd_connection(addr: std::net::SocketAddr) -> error::Result> { + Client::connect(addr) +} + #[inline] fn music_title(item: &MpsItem) -> Option { item.field("title").and_then(|x| x.to_owned().to_str()) diff --git a/src/main.rs b/src/main.rs index 082fd2d..b702664 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,7 +81,10 @@ fn main() { // build playback controller let script_file2 = script_file.clone(); let volume = args.volume.clone(); - let mpd = args.mpd.clone(); + let mpd = match args.mpd.clone().map(|a| mps_player::mpd_connection(a.parse().unwrap())).transpose() { + Ok(mpd) => mpd, + Err(e) => panic!("Abort: Cannot connect to MPD: {}", e), + }; let player_builder = move || { let script_reader = io::BufReader::new( std::fs::File::open(&script_file2) @@ -94,7 +97,7 @@ fn main() { player.set_volume(vol); } if let Some(mpd) = mpd { - player.connect_mpd(mpd.parse().unwrap()).unwrap(); + player.set_mpd(mpd); } player }; @@ -107,7 +110,7 @@ fn main() { })); match player.save_m3u8(&mut writer) { Ok(_) => println!( - "Succes: Finished playlist `{}` from script `{}`", + "Success: Finished playlist `{}` from script `{}`", playlist_file, script_file ), Err(e) => eprintln!("{}", e), @@ -116,7 +119,7 @@ fn main() { // live playback let ctrl = MpsController::create(player_builder); match ctrl.wait_for_done() { - Ok(_) => println!("Succes: Finished playback from script `{}`", script_file), + Ok(_) => println!("Success: Finished playback from script `{}`", script_file), Err(e) => eprintln!("{}", e), } } diff --git a/src/repl.rs b/src/repl.rs index bc55e89..b130735 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -47,7 +47,13 @@ pub fn repl(args: CliArgs) { term.set_title("mps"); let (writer, reader) = channel_io(); let volume = args.volume.clone(); - let mpd = args.mpd.clone(); + let mpd = match args.mpd.clone().map(|a| mps_player::mpd_connection(a.parse().unwrap())).transpose() { + Ok(mpd) => mpd, + Err(e) => { + eprintln!("Cannot connect to MPD address `{}`: {}", args.mpd.unwrap(), e); + return; + } + }; let player_builder = move || { let runner = MpsFaye::with_stream(reader); @@ -56,7 +62,7 @@ pub fn repl(args: CliArgs) { player.set_volume(vol); } if let Some(mpd) = mpd { - player.connect_mpd(mpd.parse().unwrap()).unwrap(); + player.set_mpd(mpd); } player };