Skip to content

Commit 97b8bec

Browse files
committed
feat: Support seeking / media duration in MPRIS.
1 parent 7d30a3b commit 97b8bec

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

crates/ytermusic/src/structures/media.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use flume::Sender;
24
use log::{error, info};
35
use player::Player;
@@ -54,7 +56,9 @@ impl Media {
5456
album: current.as_ref().map(|video| video.album.as_str()),
5557
artist: current.as_ref().map(|video| video.author.as_str()),
5658
cover_url: None,
57-
duration: None,
59+
duration: sink
60+
.duration()
61+
.map(|duration| Duration::from_secs(duration as u64)),
5862
};
5963
if self.current_meta
6064
!= Some((
@@ -122,7 +126,7 @@ fn connect(mpris: &mut MediaControls, sender: Sender<SoundAction>) -> Result<(),
122126
}
123127

124128
MediaControlEvent::SetPosition(a) => {
125-
todo!("Can't set position to {a:?}")
129+
sender.send(SoundAction::SeekTo(a.0)).unwrap();
126130
}
127131
MediaControlEvent::OpenUri(a) => {
128132
todo!("Implement URI opening {a:?}")

crates/ytermusic/src/structures/sound_action.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use common_structs::MusicDownloadStatus;
22
use download_manager::{DownloadManagerMessage, MessageHandler};
33
use flume::Sender;
44
use log::{error, trace};
5-
use std::{fs, sync::Arc};
5+
use std::{fs, sync::Arc, time::Duration};
66
use ytpapi2::YoutubeMusicVideoRef;
77

88
use crate::{
@@ -20,6 +20,8 @@ pub enum SoundAction {
2020
RestartPlayer,
2121
Plus,
2222
Minus,
23+
/// Seek to a specific time in the current song in seconds
24+
SeekTo(Duration),
2325
Previous(usize),
2426
Forward,
2527
Backward,
@@ -51,6 +53,7 @@ impl SoundAction {
5153

5254
pub fn apply_sound_action(self, player: &mut PlayerState) {
5355
match self {
56+
Self::SeekTo(time) => player.sink.seek_to(time),
5457
Self::Backward => player.sink.seek_bw(),
5558
Self::Forward => player.sink.seek_fw(),
5659
Self::PlayPause => player.sink.toggle_playback(),

0 commit comments

Comments
 (0)