class NowPlaying(object): # state definitions BUFFERING = 0 # forced pause to give a device's buffers time to fill up PLAYING = 1 PAUSED = 2 item = None # playable menu item (e.g. an CmAudio object) render = None state = BUFFERING start = 0 # current playback position (in milliseconds) is calculated progress = 0 # as the start position plus the progress. position should duration = 0 # of course never be greater than the duration. def __init__(self, item, duration, start=0): self.item = item self.duration = duration self.start = start self.render = NowPlayingRender() self.curry() def enter_state(self, state): if state == self.state: return if state == NowPlaying.BUFFERING: if ((self.state != NowPlaying.PLAYING) and (self.state != NowPlaying.PAUSED)): raise Exception, 'Must enter BUFFERING from PLAYING or PAUSED' elif state == NowPlaying.PLAYING: if ((self.state != NowPlaying.BUFFERING) and (self.state != NowPlaying.PAUSED)): raise Exception, 'Must enter PLAYING from BUFFERING or PAUSED' elif state == NowPlaying.PAUSED: if self.state != NowPlaying.PLAYING: raise Exception, 'Must enter PAUSED from PLAYING' elif state == NowPlaying.STOPPED: pass self.state = state def set_progress(self, progress): self.enter_state(NowPlaying.PLAYING) self.progress = progress self.render.curry(self.position() / float(self.duration), None) def paused(self): return self.state == NowPlaying.PAUSED def position(self): return self.start + self.progress def curry(self): if type(self.item) == Link: item = self.item.target else: item = self.item self.render.curry(self.position() / float(self.duration), item)
def __init__(self, item, duration, start=0): self.item = item self.duration = duration self.start = start self.render = NowPlayingRender() self.curry()