Implementing an audio seekbar
[watersofshiloah] / src / player.py
index 61ebf5c..6e61429 100644 (file)
@@ -4,6 +4,8 @@ import gobject
 
 import util.misc as misc_utils
 import stream
+import stream_index
+import call_monitor
 
 
 _moduleLogger = logging.getLogger(__name__)
@@ -37,6 +39,10 @@ class Player(gobject.GObject):
                gobject.GObject.__init__(self)
                self._index = index
                self._node = None
+               self._nextSearch = None
+
+               self._calls = call_monitor.CallMonitor()
+               self._calls.connect("call_start", self._on_call_start)
 
                self._stream = stream.GSTStream()
                self._stream.connect("state-change", self._on_stream_state)
@@ -44,14 +50,7 @@ class Player(gobject.GObject):
                self._stream.connect("error", self._on_stream_error)
 
        def set_piece_by_node(self, node):
-               assert node is None or node.is_leaf(), node
-               if self._node is node:
-                       return
-               self._node = node
-               if self._node is not None:
-                       self._stream.set_file(self._node.uri)
-               _moduleLogger.info("New node %r" % self._node)
-               self.emit("title_change", self._node)
+               self._set_piece_by_node(node)
 
        @property
        def node(self):
@@ -83,6 +82,8 @@ class Player(gobject.GObject):
                _moduleLogger.info("play")
                self._stream.play()
 
+               self._calls.start()
+
        def pause(self):
                _moduleLogger.info("pause")
                self._stream.pause()
@@ -92,11 +93,53 @@ class Player(gobject.GObject):
                self._stream.stop()
                self.set_piece_by_node(None)
 
+               self._calls.stop()
+
        def back(self):
                _moduleLogger.info("back")
+               assert self._nextSearch is None
+               self._nextSearch = stream_index.AsyncWalker(stream_index.get_previous)
+               self._nextSearch.start(self.node, self._on_next_node, self._on_node_search_error)
 
        def next(self):
                _moduleLogger.info("next")
+               assert self._nextSearch is None
+               self._nextSearch = stream_index.AsyncWalker(stream_index.get_next)
+               self._nextSearch.start(self.node, self._on_next_node, self._on_node_search_error)
+
+       def seek(self, percent):
+               target = percent * self._stream.duration
+               self._stream.seek_time(target)
+
+       @property
+       def percent_elapsed(self):
+               percent = float(self._stream.elapsed) / float(self._stream.duration)
+               return percent
+
+       def _set_piece_by_node(self, node):
+               assert node is None or node.is_leaf(), node
+               if self._node is node:
+                       _moduleLogger.info("Already set to %r" % node)
+                       return
+               self._node = node
+               if self._node is not None:
+                       self._stream.set_file(self._node.uri)
+               _moduleLogger.info("New node %r" % self._node)
+               self.emit("title_change", self._node)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_next_node(self, node):
+               self._nextSearch = None
+
+               restart = self.state == self.STATE_PLAY
+               self._set_piece_by_node(node)
+               if restart:
+                       self.play()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_node_search_error(self, e):
+               self._nextSearch = None
+               self.emit("error", e, "")
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_stream_state(self, s, state):
@@ -113,5 +156,10 @@ class Player(gobject.GObject):
                _moduleLogger.info("Error %s %s" % (error, debug))
                self.emit("error", error, debug)
 
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_call_start(self, monitor):
+               _moduleLogger.info("Call in progress, pausing")
+               self.pause()
+
 
 gobject.type_register(Player)