diff --git a/mopidy/backends/base/playback.py b/mopidy/backends/base/playback.py index 72b02a25..3c887120 100644 --- a/mopidy/backends/base/playback.py +++ b/mopidy/backends/base/playback.py @@ -312,16 +312,9 @@ class BasePlaybackController(object): original_cp_track = self.current_cp_track - # Notify frontends of the end_of_track event - self.backend.core_queue.put({ - 'to': 'frontend', - 'command': 'end_of_track', - 'track': original_cp_track[1], - }) - if self.cp_track_at_eot: + self._trigger_stopped_playing_event() self.play(self.cp_track_at_eot) - if self.random and self.current_cp_track in self._shuffled: self._shuffled.remove(self.current_cp_track) else: @@ -354,6 +347,7 @@ class BasePlaybackController(object): return if self.cp_track_at_next: + self._trigger_stopped_playing_event() self.play(self.cp_track_at_next) else: self.stop() @@ -408,12 +402,7 @@ class BasePlaybackController(object): if self.random and self.current_cp_track in self._shuffled: self._shuffled.remove(self.current_cp_track) - # Notify frontends of the now_playing event - self.backend.core_queue.put({ - 'to': 'frontend', - 'command': 'now_playing', - 'track': cp_track[1], - }) + self._trigger_started_playing_event() def _play(self, track): """ @@ -433,6 +422,7 @@ class BasePlaybackController(object): return if self.state == self.STOPPED: return + self._trigger_stopped_playing_event() self.play(self.cp_track_at_previous, on_error_step=-1) def resume(self): @@ -489,7 +479,10 @@ class BasePlaybackController(object): def stop(self): """Stop playing.""" - if self.state != self.STOPPED and self._stop(): + if self.state == self.STOPPED: + return + self._trigger_stopped_playing_event() + if self._stop(): self.state = self.STOPPED def _stop(self): @@ -500,3 +493,31 @@ class BasePlaybackController(object): :rtype: :class:`True` if successful, else :class:`False` """ raise NotImplementedError + + def _trigger_started_playing_event(self): + """ + Notifies frontends that a track has started playing. + + For internal use only. Should be called by the backend directly after a + track has started playing. + """ + self.backend.core_queue.put({ + 'to': 'frontend', + 'command': 'started_playing', + 'track': self.current_track, + }) + + def _trigger_stopped_playing_event(self): + """ + Notifies frontends that a track has stopped playing. + + For internal use only. Should be called by the backend before a track + is stopped playing, e.g. at the next, previous, and stop actions and at + end-of-track. + """ + self.backend.core_queue.put({ + 'to': 'frontend', + 'command': 'stopped_playing', + 'track': self.current_track, + 'stop_position': self.time_position, + })