def play(self, block=False, finished_cb=None): """ downloads and plays this song. if the song is already paused, it just resumes. if block is True, the song will download and play entirely before this function returns. if block is False, the song will download and play in a greenthread, so this function will return immediately """ # do we need to just resume? if self.paused: logging.info("resuming %s" % self) _pandora.resume() self.paused = False return def load_and_play(): self._play_lock.acquire() # stop anything that is currently playing _pandora.stop() self.length = _pandora.play(self.load(block=True), self.gain) logging.info("playing %s" % self) self.publish_message("playing %s" % self) finished_naturally = False preloading_next = False while True: stats = _pandora.stats() if stats: total, pos = stats self.progress = pos if pos + self.station.PRELOAD_OFFSET >= total and not preloading_next: preloading_next = True self.station.preload_next() if pos == total: self.done = True finished_naturally = True break try: self._stop_playing.get(block=True, timeout=.25) logging.debug( "got a stop signal for %s, breaking out of play loop" % self) break except eventlet.queue.Empty, e: pass logging.info("finished playing %s" % self) self.publish_message("finished playing %s" % self) self._play_lock.release() if finished_naturally and callable(finished_cb): # call the callback logging.debug("calling callback %r" % finished_cb) eventlet.spawn_n(finished_cb, self.station.account, self.station, self)
def play(self, block=False, finished_cb=None): """ downloads and plays this song. if the song is already paused, it just resumes. if block is True, the song will download and play entirely before this function returns. if block is False, the song will download and play in a greenthread, so this function will return immediately """ # do we need to just resume? if self.paused: logging.info("resuming %s" % self) _pandora.resume() self.paused = False return def load_and_play(): self._play_lock.acquire() # stop anything that is currently playing _pandora.stop() self.length = _pandora.play(self.load(block=True), self.gain) logging.info("playing %s" % self) self.publish_message("playing %s" % self) finished_naturally = False preloading_next = False while True: _pandora.update() stats = _pandora.stats() if stats: total, pos = stats self.progress = pos if pos + self.station.PRELOAD_OFFSET >= total and not preloading_next: preloading_next = True self.station.preload_next() if pos == total: self.done = True finished_naturally = True break try: self._stop_playing.get(block=True, timeout=.25) logging.debug("got a stop signal for %s, breaking out of play loop" % self) break except eventlet.queue.Empty, e: pass logging.info("finished playing %s" % self) self.publish_message("finished playing %s" % self) self._play_lock.release() if finished_naturally and callable(finished_cb): # call the callback logging.debug("calling callback %r" % finished_cb) eventlet.spawn_n(finished_cb, self.station.account, self.station, self) if block: load_and_play() else: eventlet.spawn_n(load_and_play)
def play(self, block=False): if self.paused: _pandora.resume() self.paused = False return def load_and_play(): self.length = _pandora.play(self._download()) self.publish_message("playing %s" % self) while True: stats = _pandora.stats() if stats: total, pos = stats if pos == total and account.current_station: break time.sleep(1) self.publish_message("finished playing %s" % self) if block: load_and_play() else: eventlet.spawn_n(load_and_play)