Move wall clock-based time position into Spotify backend

This commit is contained in:
Stein Magnus Jodal 2012-09-25 15:43:08 +02:00
parent 2237e4f5a1
commit 90a538c595
3 changed files with 34 additions and 52 deletions

View File

@ -1,8 +1,3 @@
import time
from mopidy.core.playback import PlaybackState
class BasePlaybackProvider(object):
"""
:param backend: the backend
@ -14,9 +9,6 @@ class BasePlaybackProvider(object):
def __init__(self, backend):
self.backend = backend
self._play_time_accumulated = 0
self._play_time_started = 0
def pause(self):
"""
Pause playback.
@ -103,40 +95,3 @@ class BasePlaybackProvider(object):
:type volume: int [0..100]
"""
self.backend.audio.set_volume(volume)
def wall_clock_based_time_position(self):
"""
Helper method that tracks track time position using the wall clock.
To use this helper you must call the helper from your implementation of
:meth:`get_time_position` and return its return value.
:rtype: int
"""
state = self.backend.playback.state
if state == PlaybackState.PLAYING:
time_since_started = (self._wall_time() -
self._play_time_started)
return self._play_time_accumulated + time_since_started
elif state == PlaybackState.PAUSED:
return self._play_time_accumulated
elif state == PlaybackState.STOPPED:
return 0
def update_play_time_on_play(self):
self._play_time_accumulated = 0
self._play_time_started = self._wall_time()
def update_play_time_on_pause(self):
time_since_started = self._wall_time() - self._play_time_started
self._play_time_accumulated += time_since_started
def update_play_time_on_resume(self):
self._play_time_started = self._wall_time()
def update_play_time_on_seek(self, time_position):
self._play_time_started = self._wall_time()
self._play_time_accumulated = time_position
def _wall_time(self):
return int(time.time() * 1000)

View File

@ -1,4 +1,5 @@
import logging
import time
from spotify import Link, SpotifyError
@ -10,11 +11,25 @@ logger = logging.getLogger('mopidy.backends.spotify.playback')
class SpotifyPlaybackProvider(BasePlaybackProvider):
def __init__(self, *args, **kwargs):
super(SpotifyPlaybackProvider, self).__init__(*args, **kwargs)
self._play_time_accumulated = 0
self._play_time_started = 0
def pause(self):
time_since_started = self._wall_time() - self._play_time_started
self._play_time_accumulated += time_since_started
return super(SpotifyPlaybackProvider, self).pause()
def play(self, track):
if self.backend.playback.state == PlaybackState.PLAYING:
self.backend.spotify.session.play(0)
if track.uri is None:
return False
self._play_time_accumulated = 0
self._play_time_started = self._wall_time()
try:
self.backend.spotify.session.load(
Link.from_string(track.uri).as_track())
@ -29,12 +44,17 @@ class SpotifyPlaybackProvider(BasePlaybackProvider):
return False
def resume(self):
self._play_time_started = self._wall_time()
return self.seek(self.backend.playback.time_position)
def seek(self, time_position):
self._play_time_started = self._wall_time()
self._play_time_accumulated = time_position
self.backend.audio.prepare_change()
self.backend.spotify.session.seek(time_position)
self.backend.audio.start_playback()
return True
def stop(self):
@ -46,4 +66,15 @@ class SpotifyPlaybackProvider(BasePlaybackProvider):
# when used with the Spotify backend and GStreamer appsrc. If this can
# be resolved, we no longer need to use a wall clock based time
# position for Spotify playback.
return self.wall_clock_based_time_position()
state = self.backend.playback.state
if state == PlaybackState.PLAYING:
time_since_started = (self._wall_time() -
self._play_time_started)
return self._play_time_accumulated + time_since_started
elif state == PlaybackState.PAUSED:
return self._play_time_accumulated
elif state == PlaybackState.STOPPED:
return 0
def _wall_time(self):
return int(time.time() * 1000)

View File

@ -370,7 +370,6 @@ class PlaybackController(object):
"""Pause playback."""
if self.provider.pause():
self.state = PlaybackState.PAUSED
self.provider.update_play_time_on_pause()
self._trigger_track_playback_paused()
def play(self, cp_track=None, on_error_step=1):
@ -413,7 +412,6 @@ class PlaybackController(object):
if self.random and self.current_cp_track in self._shuffled:
self._shuffled.remove(self.current_cp_track)
self.provider.update_play_time_on_play()
self._trigger_track_playback_started()
def previous(self):
@ -430,7 +428,6 @@ class PlaybackController(object):
"""If paused, resume playing the current track."""
if self.state == PlaybackState.PAUSED and self.provider.resume():
self.state = PlaybackState.PLAYING
self.provider.update_play_time_on_resume()
self._trigger_track_playback_resumed()
def seek(self, time_position):
@ -457,7 +454,6 @@ class PlaybackController(object):
success = self.provider.seek(time_position)
if success:
self.provider.update_play_time_on_seek(time_position)
self._trigger_seeked(time_position)
return success