This commit is contained in:
Erica Z 2024-11-01 20:04:07 +01:00
parent a8776faa1f
commit 7f20e422d7
2 changed files with 60 additions and 18 deletions

View file

@ -43,7 +43,7 @@ impl MetadataMap {
.try_into() .try_into()
.unwrap() .unwrap()
}), }),
//length: Some((song.duration() * MICROSECONDS) as i64), length: Some(song.duration() * MICROSECONDS as i64),
album: Some(song.album().into()), album: Some(song.album().into()),
artist: Some(vec![song.artist().into()]), artist: Some(vec![song.artist().into()]),
//content_created: song.year().map(|year| chrono::NaiveDate::from_yo_opt(year, 1).unwrap()), // FIXME: replace this unwrap with Some(Err) -> None //content_created: song.year().map(|year| chrono::NaiveDate::from_yo_opt(year, 1).unwrap()), // FIXME: replace this unwrap with Some(Err) -> None
@ -97,7 +97,6 @@ impl MetadataMap {
pub struct Player { pub struct Player {
playbin: SendWeakRef<crate::Playbin>, playbin: SendWeakRef<crate::Playbin>,
metadata: MetadataMap, metadata: MetadataMap,
volume: f64,
} }
impl Player { impl Player {
@ -110,7 +109,6 @@ impl Player {
let player = Self { let player = Self {
playbin: playbin.downgrade().into(), playbin: playbin.downgrade().into(),
metadata: MetadataMap::from_playbin_song(None), metadata: MetadataMap::from_playbin_song(None),
volume: (playbin.volume() as f64) / 100.0,
}; };
object_server.at("/org/mpris/MediaPlayer2", player).await?; object_server.at("/org/mpris/MediaPlayer2", player).await?;
@ -141,6 +139,24 @@ impl Player {
), ),
); );
playbin.connect_closure(
"seeked",
false,
glib::closure_local!(
#[strong]
player_ref,
move |_playbin: &crate::Playbin, position: f64| {
let player_ref = player_ref.clone();
glib::spawn_future_local(async move {
player_ref
.seeked((position * MICROSECONDS) as i64)
.await
.unwrap();
});
}
),
);
playbin.connect_notify_local( playbin.connect_notify_local(
Some("play-queue-length"), Some("play-queue-length"),
glib::clone!( glib::clone!(
@ -192,13 +208,10 @@ impl Player {
glib::clone!( glib::clone!(
#[strong] #[strong]
player_ref, player_ref,
move |playbin: &crate::Playbin, _| { move |_playbin: &crate::Playbin, _| {
let playbin_volume = playbin.volume();
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { glib::spawn_future_local(async move {
let mut player = player_ref.get_mut().await; let player = player_ref.get_mut().await;
player.volume = (playbin_volume as f64) / 100.0;
player player
.volume_changed(player_ref.signal_emitter()) .volume_changed(player_ref.signal_emitter())
.await .await
@ -223,32 +236,43 @@ impl Player {
let playbin = self.playbin.upgrade().unwrap(); let playbin = self.playbin.upgrade().unwrap();
if playbin.play_queue_position() + 1 > playbin.play_queue_length() { if playbin.play_queue_position() + 1 > playbin.play_queue_length() {
// If there is no next track (and endless playback and track repeat are both off), stop playback. // If there is no next track (and endless playback and track repeat are both off), stop playback.
self.stop(); // (interpret this as something else than what Stop does)
playbin.stop();
} else { } else {
playbin.go_to_next_track(); playbin.go_to_next_track();
} }
} }
fn previous(&self) { fn previous(&self) {
let playbin = self.playbin.upgrade().unwrap();
// If CanGoPrevious is false, attempting to call this method should have no effect. // If CanGoPrevious is false, attempting to call this method should have no effect.
if !self.can_go_previous() { if !self.can_go_previous() {
return; return;
} }
let playbin = self.playbin.upgrade().unwrap();
if playbin.play_queue_position() == 0 { if playbin.play_queue_position() == 0 {
// If there is no previous track (and endless playback and track repeat are both off), stop playback. // If there is no previous track (and endless playback and track repeat are both off), stop playback.
self.stop(); // (interpret this as something else than what Stop does)
playbin.stop();
} else { } else {
playbin.go_to_prev_track(); playbin.go_to_prev_track();
} }
} }
fn pause(&self) { fn pause(&self) {
let playbin = self.playbin.upgrade().unwrap();
// If CanPause is false, attempting to call this method should have no effect. // If CanPause is false, attempting to call this method should have no effect.
if !self.can_pause() { if !self.can_pause() {
return; return;
} }
// If playback is already paused, this has no effect.
if playbin.state() != crate::playbin::State::Playing {
return;
}
self.playbin.upgrade().unwrap().pause(); self.playbin.upgrade().unwrap().pause();
} }
@ -262,18 +286,31 @@ impl Player {
} }
fn stop(&self) { fn stop(&self) {
// TODO: Calling Play after this should cause playback to start again from the beginning of the track. let playbin = self.playbin.upgrade().unwrap();
// (not the play queue!!)
self.playbin.upgrade().unwrap().stop(); // If playback is already stopped, this has no effect.
if playbin.state() != crate::playbin::State::Playing {
return;
}
// Calling Play after this should cause playback to start again from the beginning of the track.
playbin.pause();
playbin.seek(0.0);
} }
fn play(&self) { fn play(&self) {
let playbin = self.playbin.upgrade().unwrap();
// If CanPlay is false, attempting to call this method should have no effect. // If CanPlay is false, attempting to call this method should have no effect.
if !self.can_play() { if !self.can_play() {
return; return;
} }
let playbin = self.playbin.upgrade().unwrap(); // If already playing, this has no effect.
if playbin.state() == crate::playbin::State::Playing {
return;
}
// If there is no track to play, this has no effect. // If there is no track to play, this has no effect.
if playbin.play_queue_length() == 0 { if playbin.play_queue_length() == 0 {
return; return;
@ -351,8 +388,8 @@ impl Player {
} }
#[zbus(property)] #[zbus(property)]
fn set_loop_status(&self, _loop_status: &str) { fn set_loop_status(&self, _loop_status: &str) -> zbus::Result<()> {
todo!() Err(zbus::Error::Unsupported) // TODO
} }
#[zbus(property)] #[zbus(property)]
@ -387,7 +424,7 @@ impl Player {
#[zbus(property)] #[zbus(property)]
fn volume(&self) -> f64 { fn volume(&self) -> f64 {
1.0 // TODO self.playbin.upgrade().unwrap().volume() as f64 / 100.0
} }
#[zbus(property)] #[zbus(property)]

View file

@ -25,6 +25,7 @@ pub mod ffi {
pub fn audrey_playbin_song_get_album( pub fn audrey_playbin_song_get_album(
self_: *mut AudreyPlaybinSong, self_: *mut AudreyPlaybinSong,
) -> *const std::ffi::c_char; ) -> *const std::ffi::c_char;
pub fn audrey_playbin_song_get_duration(self_: *mut AudreyPlaybinSong) -> i64;
} }
} }
@ -60,4 +61,8 @@ impl Song {
pub fn album(&self) -> GString { pub fn album(&self) -> GString {
unsafe { from_glib_none(ffi::audrey_playbin_song_get_album(self.to_glib_none().0)) } unsafe { from_glib_none(ffi::audrey_playbin_song_get_album(self.to_glib_none().0)) }
} }
pub fn duration(&self) -> i64 {
unsafe { ffi::audrey_playbin_song_get_duration(self.to_glib_none().0) }
}
} }