[gnome-music/wip/jfelder/smoothscale-player: 10/14] player: Define duration GObject property



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

    player: Define duration GObject property
    
    Change default duration value to zero to be able to define the
    property as an int.
    This property will 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 b8772bbc..1a37f25a 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')
@@ -132,7 +132,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
@@ -165,7 +165,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):
@@ -267,7 +267,7 @@ class GstPlayer(GObject.GObject):
 
         return position
 
-    @GObject.Property
+    @GObject.Property(type=int)
     def duration(self):
         """Total duration of current media
 
@@ -276,7 +276,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 7f871f95..766bb919 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)
@@ -738,15 +742,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)
 
@@ -820,11 +823,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]