Add player interpret and track
This commit is contained in:
@@ -6,3 +6,5 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = "1.0.81"
|
||||||
|
mpris = "2.0.1"
|
||||||
|
14
src/main.rs
14
src/main.rs
@@ -1,3 +1,17 @@
|
|||||||
|
mod player;
|
||||||
|
|
||||||
|
use mpris::PlayerFinder;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
let player = PlayerFinder::new()
|
||||||
|
.expect("Could not connect to D-Bus")
|
||||||
|
.find_active()
|
||||||
|
.expect("Could not find any player");
|
||||||
|
|
||||||
|
let metadata = player.get_metadata().expect("Could not find metadata");
|
||||||
|
if let (Some(title), Some(artists)) = (metadata.title(), metadata.artists()) {
|
||||||
|
let artist = artists.join(", ");
|
||||||
|
println!("Current track: {artist} - {title}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
24
src/player.rs
Normal file
24
src/player.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use mpris::PlayerFinder;
|
||||||
|
|
||||||
|
pub struct MprisPlayer {
|
||||||
|
player_finder: PlayerFinder,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MprisPlayer {
|
||||||
|
pub fn new() -> Result<Self> {
|
||||||
|
let player_finder = PlayerFinder::new()?;
|
||||||
|
Ok(MprisPlayer { player_finder })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_interpret_and_track(&self) -> Result<String> {
|
||||||
|
let player = self.player_finder.find_active()?;
|
||||||
|
|
||||||
|
let metadata = player.get_metadata()?;
|
||||||
|
if let (Some(title), Some(artists)) = (metadata.title(), metadata.artists()) {
|
||||||
|
let artist = artists.join(", ");
|
||||||
|
return Ok(format!("{artist} - {title}"));
|
||||||
|
}
|
||||||
|
Err(anyhow!("Could not create interpret and title string"))
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user