[gnome-music/wip/jfelder/smoothscale-player: 3/6] player: Define duration GObject property



commit af13b384285180fb502f8f6e972e58c9424f95f2
Author: Jean Felder <jfelder src gnome org>
Date:   Thu Oct 4 08:10:45 2018 +0200

    player: Define duration GObject property
    
    This property will also be used by Smoothscale.

 gnomemusic/gstplayer.py | 10 +++++-----
 gnomemusic/player.py    | 16 +++++++++-------
 2 files changed, 14 insertions(+), 12 deletions(-)
---
diff --git a/gnomemusic/gstplayer.py b/gnomemusic/gstplayer.py
index e3c7e028..af7ed175 100644
--- a/gnomemusic/gstplayer.py
+++ b/gnomemusic/gstplayer.py
@@ -66,7 +66,7 @@ class GstPlayer(GObject.GObject):
 
         Gst.init(None)
 
-        self._duration = None
+        self._duration = 0
 
         self._missing_plugin_messages = []
         self._settings = Gio.Settings.new('org.gnome.Music')
@@ -131,7 +131,7 @@ class GstPlayer(GObject.GObject):
 
         # TODO: Workaround the first duration change not being emitted
         # and hence smoothscale not being initialized properly.
-        if self.duration is None:
+        if self.duration == 0:
             self._on_duration_changed(None, None)
 
     @log
@@ -159,7 +159,7 @@ class GstPlayer(GObject.GObject):
         if success:
             self.duration = duration / Gst.SECOND
         else:
-            self.duration = None
+            self.duration = 0
 
     @log
     def _on_bus_element(self, bus, message):
@@ -259,7 +259,7 @@ class GstPlayer(GObject.GObject):
 
         return position
 
-    @GObject.Property
+    @GObject.Property(type=int)
     def duration(self):
         """Total duration of current media
 
@@ -268,7 +268,7 @@ class GstPlayer(GObject.GObject):
         :rtype: float or None
         """
         if self.state == Playback.STOPPED:
-            return None
+            return 0
 
         return self._duration
 
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 0d266b4a..93fa9260 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -506,6 +506,7 @@ class Player(GObject.GObject):
     }
 
     state = GObject.Property(type=int, default=Playback.STOPPED)
+    duration = GObject.Property(type=int, default=0)
 
     def __repr__(self):
         return '<Player>'
@@ -531,6 +532,9 @@ class Player(GObject.GObject):
         self._player = GstPlayer()
         self._player.connect('clock-tick', self._on_clock_tick)
         self._player.connect('eos', self._on_eos)
+        self._player.bind_property(
+            'duration', self, 'duration',
+            GObject.BindingFlags.SYNC_CREATE)
         self._player.bind_property(
             'state', self, 'state',
             GObject.BindingFlags.SYNC_CREATE |
@@ -740,15 +744,14 @@ class Player(GObject.GObject):
             self._new_clock = True
             self._lastfm.now_playing(current_song)
 
-        duration = self._player.duration
-        if duration is None:
+        if self.props.duration == 0:
             return
 
         position = self._player.position
         if position > 0:
-            percentage = tick / duration
+            percentage = tick / self.props.duration
             if (not self._lastfm.scrobbled
-                    and duration > 30
+                    and self.props.duration > 30
                     and (percentage > 0.5 or tick > 4 * 60)):
                 self._lastfm.scrobble(current_song, self._time_stamp)
 
@@ -822,11 +825,10 @@ class Player(GObject.GObject):
             else:
                 return
 
-        duration = self._player.duration
-        if duration is None:
+        if self.props.duration == 0:
             return
 
-        if duration >= offset * 1000:
+        if self.props.duration >= offset * 1000:
             self._player.seek(offset * 1000)
             self.emit('seeked', offset)
         elif next_on_overflow:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]