From 63fb6d96d2740fe3641a1a24ef6ffb720ec928c7 Mon Sep 17 00:00:00 2001 From: Louis Brauer Date: Fri, 16 Dec 2022 11:12:43 +0100 Subject: [PATCH] Add current station song title and artwork to MPRIS mediaplayer --- src/Controllers/PlayerController.vala | 2 ++ src/Services/DBusMediaPlayer.vala | 37 +++++++++++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Controllers/PlayerController.vala b/src/Controllers/PlayerController.vala index 3653b8f..c078b90 100644 --- a/src/Controllers/PlayerController.vala +++ b/src/Controllers/PlayerController.vala @@ -8,6 +8,7 @@ public class Tuner.PlayerController : Object { private Model.Station _station; private Gst.PlayerState? _current_state = Gst.PlayerState.STOPPED; public Gst.Player player; + public string currentTitle = " "; public signal void station_changed (Model.Station station); public signal void state_changed (Gst.PlayerState state); @@ -27,6 +28,7 @@ public class Tuner.PlayerController : Object { string? title = extract_title_from_stream (obj); if (title != null) { debug(@"Got new title from station: $title"); + currentTitle = title; title_changed(title); } }); diff --git a/src/Services/DBusMediaPlayer.vala b/src/Services/DBusMediaPlayer.vala index 3d9e3f7..03349a4 100644 --- a/src/Services/DBusMediaPlayer.vala +++ b/src/Services/DBusMediaPlayer.vala @@ -79,7 +79,7 @@ namespace Tuner.DBus { public string identity { owned get { - return "tuner@exe"; + return "Tuner"; } } @@ -109,6 +109,9 @@ namespace Tuner.DBus { public class MediaPlayerPlayer : Object, DBus.IMediaPlayer2Player { [DBus (visible = false)] private string _playback_status = "Stopped"; + private string _current_title = ""; + private string _current_artist = "Tuner"; + private string? _current_art_url = null; private uint update_metadata_source = 0; private uint send_property_source = 0; private HashTable changed_properties = null; @@ -134,6 +137,19 @@ namespace Tuner.DBus { break; } }); + + Application.instance.player.title_changed.connect ((title) => { + _current_title = title; + trigger_metadata_update (); + }); + + Application.instance.player.station_changed.connect ((station) => { + _current_title = station.title; + _current_artist = station.title; + _current_art_url = station.favicon_url; + trigger_metadata_update (); + }); + } public void next() throws DBusError, IOError { @@ -202,17 +218,16 @@ namespace Tuner.DBus { owned get { // debug ("DBus metadata requested"); var table = new HashTable (str_hash, str_equal); - table.insert ("xesam:title", "Tuner"); - - var station = Application.instance.player.station; - if (station != null) { - var station_title = station.title; - table.insert ("xesam:artist", get_simple_string_array (station_title)); - } else { - table.insert ("xesam:artist", get_simple_string_array (null)); - } + table.insert ("xesam:title", _current_title); + table.insert ("xesam:artist", get_simple_string_array (_current_artist)); + + // this is necessary to remove previous images if the current + // station has none + var art = _current_art_url == null || _current_art_url == "" ? "file:///" : _current_art_url; + + table.insert ("mpris:artUrl", art); - return table; + return table; } } public double volume { owned get; set; }