Skip to content

Commit

Permalink
Add current station song title and artwork to MPRIS mediaplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
louis77 committed Dec 16, 2022
1 parent 20d52bf commit 63fb6d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Controllers/PlayerController.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
});
Expand Down
37 changes: 26 additions & 11 deletions src/Services/DBusMediaPlayer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace Tuner.DBus {

public string identity {
owned get {
return "tuner@exe";
return "Tuner";
}
}

Expand Down Expand Up @@ -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<string,Variant> changed_properties = null;
Expand All @@ -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 {
Expand Down Expand Up @@ -202,17 +218,16 @@ namespace Tuner.DBus {
owned get {
// debug ("DBus metadata requested");
var table = new HashTable<string, Variant> (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; }
Expand Down

1 comment on commit 63fb6d9

@louis77
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to #97

Please sign in to comment.