use mpris::{PlaybackStatus, PlayerFinder};
fn main() {
match play_pause() {
Ok(playback_status) => match playback_status {
PlaybackStatus::Playing => println!("Player is now playing."),
PlaybackStatus::Paused => println!("Player is now paused."),
PlaybackStatus::Stopped => println!("Player is stopped."),
},
Err(error) => {
println!("ERROR: {}", error);
std::process::exit(1);
}
}
}
fn play_pause() -> Result<PlaybackStatus, String> {
let player_finder =
PlayerFinder::new().map_err(|e| format!("Could not connect to D-Bus: {}", e))?;
let player = player_finder
.find_active()
.map_err(|e| format!("Could not find any player: {}", e))?;
let toggled = player
.checked_play_pause()
.map_err(|e| format!("Could not control player: {}", e))?;
if toggled {
std::thread::sleep(std::time::Duration::from_millis(50));
player
.get_playback_status()
.map_err(|e| format!("Could not get playback status: {}", e))
} else {
Err(String::from("Media cannot be paused"))
}
}