can now set volume again thru mpris

This commit is contained in:
Erica Z 2024-11-01 15:17:37 +01:00
parent 6272c2ce07
commit d873d03973
2 changed files with 39 additions and 16 deletions

View file

@ -12,24 +12,24 @@ struct MetadataMap {
}
impl MetadataMap {
fn as_hash_map(&self) -> HashMap<&'static str, Value> {
HashMap::from([("mpris:trackid", Value::new(self.trackid.as_ref()))])
}
}
impl Default for MetadataMap {
fn default() -> Self {
fn from_playbin(_playbin: &crate::Playbin) -> Self {
// TODO
Self {
trackid: "/org/mpris/MediaPlayer2/TrackList/NoTrack"
.try_into()
.unwrap(),
}
}
fn as_hash_map(&self) -> HashMap<&'static str, Value> {
HashMap::from([("mpris:trackid", Value::new(self.trackid.as_ref()))])
}
}
pub struct Player {
playbin: SendWeakRef<crate::Playbin>,
metadata: MetadataMap,
volume: f64,
}
impl Player {
@ -41,12 +41,13 @@ impl Player {
let player = Self {
playbin: playbin.downgrade().into(),
metadata: Default::default(),
metadata: MetadataMap::from_playbin(playbin),
volume: (playbin.volume() as f64) / 100.0,
};
object_server.at("/org/mpris/MediaPlayer2", player).await?;
let player = object_server
let player_ref = object_server
.interface::<_, Self>("/org/mpris/MediaPlayer2")
.await?;
@ -54,15 +55,35 @@ impl Player {
"new-track",
false,
glib::closure_local!(
#[strong]
player,
//#[strong]
//player,
move |_playbin: &crate::Playbin| {
let _player = player.get_mut();
// TODO
}
),
);
playbin.connect_notify_local(
Some("volume"),
glib::clone!(
#[strong]
player_ref,
move |playbin: &crate::Playbin, _| {
let playbin_volume = playbin.volume();
let player_ref = player_ref.clone();
glib::spawn_future_local(async move {
let mut player = player_ref.get_mut().await;
player.volume = (playbin_volume as f64) / 100.0;
player
.volume_changed(player_ref.signal_emitter())
.await
.unwrap();
});
}
),
);
Ok(())
}
}
@ -158,8 +179,10 @@ impl Player {
}
#[zbus(property)]
fn set_volume(&self, _volume: f64) {
todo!()
fn set_volume(&mut self, volume: f64) {
let playbin = self.playbin.upgrade().unwrap();
// FIXME: check if this is set by the notify callback: self.volume = volume;
playbin.set_volume((volume * 100.0) as i32);
}
#[zbus(property(emits_changed_signal = "false"))]
@ -204,6 +227,6 @@ impl Player {
#[zbus(property(emits_changed_signal = "const"))]
fn can_control(&self) -> bool {
false // TODO
true
}
}

View file

@ -77,7 +77,7 @@ public class Audrey.Playbin : GLib.Object {
if (ret >= 0) {
_volume = value;
} else {
warning ("failed to set volume: %s", ret.to_string ());
warning ("failed to set volume to %d: %s", value, ret.to_string ());
}
}
}