Skip to content

Commit

Permalink
started adding mpris lenght
Browse files Browse the repository at this point in the history
  • Loading branch information
Nokse22 committed Jun 1, 2024
1 parent 143dae4 commit a75adab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/lib/player_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,20 @@ class playerObject(GObject.GObject):
'song-changed': (GObject.SignalFlags.RUN_FIRST, None, ()),
'song-added-to-queue': (GObject.SignalFlags.RUN_FIRST, None, ()),
'play-changed': (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
'duration-changed': (GObject.SignalFlags.RUN_FIRST, None, ()),
}

def __init__(self):
GObject.GObject.__init__(self)

# TODO Rename all player_object to something like GstPlayer

Gst.init()

self._player = Gst.ElementFactory.make('playbin3', 'player')
self._bus = self._player.get_bus()
self._bus.add_signal_watch()

self.queue = [] # List to store queued songs (Not the next songs in an album/playlist/mix, but the ones added with play next/add to queue)

self.current_mix_album_playlist = None # Information about the currently playing mix/album
Expand All @@ -65,17 +75,11 @@ def __init__(self):
self.playing_track = None
self.song_album = None

self.duration = self.query_duration()

self.can_next = False
self.can_prev = False

Gst.init()

# TODO Rename all player_object to something like GstPlayer

self._player = Gst.ElementFactory.make('playbin3', 'player')
self._bus = self._player.get_bus()
self._bus.add_signal_watch()

self._bus.connect('message::eos', self._on_bus_eos)

# ---------------------------- FROM GNOME-MUSIC ----------------------------
Expand Down Expand Up @@ -172,6 +176,8 @@ def _play_track(self, track):

self._player.set_property("uri", music_url)

self.duration = self.query_duration()

if self.is_playing:
self.play()

Expand Down Expand Up @@ -298,15 +304,29 @@ def set_current_mix_album_playlist(self, mix_album):

def update_slider_call(self):
self.emit("update-slider")

duration = self.query_duration()
if duration != self.duration:
self.emit("duration-changed")

if self.is_playing:
return True
return False

def query_duration(self, time_format):
return self._player.query_duration(Gst.Format.TIME)
def query_duration(self):
success, duration = self._player.query_duration(Gst.Format.TIME)
if success:
print(duration)
return duration
else:
return 0

def query_position(self, time_format):
return self._player.query_position(Gst.Format.TIME)
def query_position(self):
success, position = self._player.query_position(Gst.Format.TIME)
if success:
return position
else:
return 0

def seek(self, time_format, something, seek_time):
self._player.seek_simple(time_format, something, seek_time)
3 changes: 3 additions & 0 deletions src/mpris.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def __init__(self, player):
self.__metadata["xesam:title"] = GLib.Variant("s", track.name)
self.__metadata["xesam:album"] = GLib.Variant("s", track.album)
self.__metadata["xesam:artist"] = GLib.Variant("as", [track.artist])
self.__metadata["mpris:length"] = GLib.Variant("i", self.player.query_duration())

self.__bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
Gio.bus_own_name_on_connection(
Expand All @@ -161,6 +162,7 @@ def __init__(self, player):
Server.__init__(self, self.__bus, self.__MPRIS_PATH)

self.player.connect("song-changed", self._on_preset_changed)
self.player.connect("duration-changed", self._on_preset_changed)
self.player.connect("play-changed", self._on_playing_changed)
# MainPlayer.get().connect("notify::volume", self._on_volume_changed)

Expand Down Expand Up @@ -268,6 +270,7 @@ def _on_preset_changed(self, *args):
self.__metadata["xesam:title"] = GLib.Variant("s", self.player.playing_track.name)
self.__metadata["xesam:album"] = GLib.Variant("s", self.player.playing_track.album.name)
self.__metadata["xesam:artist"] = GLib.Variant("as", [self.player.playing_track.artist.name])
self.__metadata["mpris:length"] = GLib.Variant("i", self.player.duration)

url = f"file://{variables.IMG_DIR}/{self.player.playing_track.album.id}.jpg"

Expand Down

0 comments on commit a75adab

Please sign in to comment.