Added MPRIS OpenUri support#993
Open
Bettehem wants to merge 1 commit into
Open
Conversation
MPRIS OpenUri now works with Spotify URIs, for example: spotify:track:6CfwYtEnSDgMxosLZ5Vbiu spotify:album:3HNzOyPbz5vPvUie7lI97X
mielpeeters
reviewed
May 30, 2026
Comment on lines
+142
to
+173
| 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<Playback> = 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(); | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
I would implement it like this:
Suggested change
| 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<Playback> = 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(); | |
| } | |
| } | |
| MediaControlEvent::OpenUri(uri) => { | |
| let mut split = uri.split(':'); | |
| let (Some("spotify"), Some(uri_type), Some(id)) = | |
| (split.next(), split.next(), split.next()) | |
| else { | |
| return; | |
| }; | |
| let id = id.to_string(); | |
| let playback = match uri_type { | |
| "album" => AlbumId::from_id(id) | |
| .ok() | |
| .map(|album_id| Playback::Context(ContextId::Album(album_id), None)), | |
| "track" => TrackId::from_id(id) | |
| .ok() | |
| .map(|track_id| Playback::URIs(vec![PlayableId::Track(track_id)], None)), | |
| "playlist" => PlaylistId::from_id(id).ok().map(|playlist_id| { | |
| Playback::Context(ContextId::Playlist(playlist_id), None) | |
| }), | |
| "show" => ShowId::from_id(id) | |
| .ok() | |
| .map(|show_id| Playback::Context(ContextId::Show(show_id), None)), | |
| "episode" => EpisodeId::from_id(id).ok().map(|episode_id| { | |
| Playback::URIs(vec![PlayableId::Episode(episode_id)], None) | |
| }), | |
| _ => None, | |
| }; | |
| if let Some(playback) = playback { | |
| client_pub | |
| .send(ClientRequest::Player(PlayerRequest::StartPlayback( | |
| playback, None, | |
| ))) | |
| .unwrap_or_default(); | |
| } | |
| } | |
I'd argue that especially the parsing of the URI is more readable this way, plus it checks that the first part is actually 'spotify'.
The changes in the match statement get rid of the unwrap() calls you had, which is more safe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implemented MPRIS OpenUri and it now works with Spotify URIs, for example:
spotify:track:6CfwYtEnSDgMxosLZ5Vbiuand
spotify:album:3HNzOyPbz5vPvUie7lI97XI'm still a beginner at rust so please let me know if there is anything that should be done differently