From b2dc0be3856f74bdab7fe0b41fb8e8e07e963277 Mon Sep 17 00:00:00 2001 From: Bettehem Date: Wed, 20 May 2026 01:22:56 +0300 Subject: [PATCH] media_control: add MPRIS OpenUri MPRIS OpenUri now works with Spotify URIs, for example: spotify:track:6CfwYtEnSDgMxosLZ5Vbiu spotify:album:3HNzOyPbz5vPvUie7lI97X --- spotify_player/src/media_control.rs | 49 ++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/spotify_player/src/media_control.rs b/spotify_player/src/media_control.rs index 9b443278..6c16eb6e 100644 --- a/spotify_player/src/media_control.rs +++ b/spotify_player/src/media_control.rs @@ -1,7 +1,12 @@ #![allow(unused_imports)] +use chrono::TimeDelta; +use rspotify::model::{ + AlbumId, EpisodeId, Offset, PlayContextId, PlayableId, PlaylistId, ShowId, TrackId, +}; use souvlaki::MediaPosition; use souvlaki::{MediaControlEvent, MediaControls, MediaMetadata, MediaPlayback, PlatformConfig}; +use crate::state::{ContextId, Playback}; use crate::utils; use crate::{ client::{ClientRequest, PlayerRequest}, @@ -127,11 +132,45 @@ pub fn start_event_watcher( .send(ClientRequest::Player(PlayerRequest::PreviousTrack)) .unwrap_or_default(); } - MediaControlEvent::SetVolume(volume) => client_pub - .send(ClientRequest::Player(PlayerRequest::Volume( - (volume * 100.0) as u8, - ))) - .unwrap_or_default(), + MediaControlEvent::SetVolume(volume) => { + client_pub + .send(ClientRequest::Player(PlayerRequest::Volume( + (volume * 100.0) as u8, + ))) + .unwrap_or_default(); + } + MediaControlEvent::OpenUri(uri) => { + let id = uri[uri.rfind(":").unwrap_or(0) + 1..uri.len()].to_string(); + let uri_type = &uri[uri.find(":").unwrap_or(0) + 1..uri.rfind(":").unwrap_or(0)]; + let playback: Option = match uri_type { + "album" => { + let album_id = AlbumId::from_id(id).unwrap(); + Some(Playback::Context(ContextId::Album(album_id), None)) + } + "track" => { + let track_id = PlayableId::Track(TrackId::from_id(id).unwrap()); + Some(Playback::URIs(vec![track_id], None)) + } + "playlist" => { + let playlist_id = PlaylistId::from_id(id).unwrap(); + Some(Playback::Context(ContextId::Playlist(playlist_id), None)) + } + "show" => { + let show_id = ShowId::from_id(id).unwrap(); + Some(Playback::Context(ContextId::Show(show_id), None)) + } + "episode" => { + let episode_id = PlayableId::Episode(EpisodeId::from_id(id).unwrap()); + Some(Playback::URIs(vec![episode_id], None)) + } + _ => None, + }; + if let Some(s) = playback { + client_pub + .send(ClientRequest::Player(PlayerRequest::StartPlayback(s, None))) + .unwrap_or_default(); + } + } _ => {} } })?;