mpvipc-async/examples/media_player.rs
h7x4 2ed8025046
All checks were successful
Build and test / check (pull_request) Successful in 1m58s
Build and test / build (pull_request) Successful in 1m59s
Build and test / docs (pull_request) Successful in 2m44s
Build and test / test (pull_request) Successful in 3m19s
Build and test / build (push) Successful in 1m56s
Build and test / check (push) Successful in 1m50s
Build and test / docs (push) Successful in 2m45s
Build and test / test (push) Successful in 3m29s
fix examples and documentation
2024-05-04 00:23:02 +02:00

64 lines
2.4 KiB
Rust

use futures::StreamExt;
use mpvipc::{parse_event_property, Event, Mpv, MpvDataType, MpvError, MpvExt, Property};
fn seconds_to_hms(total: f64) -> String {
let total = total as u64;
let seconds = total % 60;
let total = total / 60;
let minutes = total % 60;
let hours = total / 60;
format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
}
#[tokio::main]
async fn main() -> Result<(), MpvError> {
env_logger::init();
let mpv = Mpv::connect("/tmp/mpv.sock").await?;
mpv.observe_property(1, "path").await?;
mpv.observe_property(2, "pause").await?;
mpv.observe_property(3, "playback-time").await?;
mpv.observe_property(4, "duration").await?;
mpv.observe_property(5, "metadata").await?;
let mut events = mpv.get_event_stream().await;
while let Some(Ok(event)) = events.next().await {
match event {
mpvipc::Event::PropertyChange { .. } => match parse_event_property(event)? {
(1, Property::Path(Some(value))) => println!("\nPlaying: {}", value),
(2, Property::Pause(value)) => {
println!("Pause: {}", value);
}
(3, Property::PlaybackTime(Some(value))) => {
println!("Playback time: {}", seconds_to_hms(value));
}
(4, Property::Duration(Some(value))) => {
println!("Duration: {}", seconds_to_hms(value));
}
(5, Property::Metadata(Some(value))) => {
println!("File tags:");
if let Some(MpvDataType::String(value)) = value.get("ARTIST") {
println!(" Artist: {}", value);
}
if let Some(MpvDataType::String(value)) = value.get("ALBUM") {
println!(" Album: {}", value);
}
if let Some(MpvDataType::String(value)) = value.get("TITLE") {
println!(" Title: {}", value);
}
if let Some(MpvDataType::String(value)) = value.get("TRACK") {
println!(" Track: {}", value);
}
}
_ => (),
},
Event::Shutdown => return Ok(()),
Event::Unimplemented(_) => panic!("Unimplemented event"),
_ => (),
}
}
Ok(())
}