Chromecast support mainly working

This commit is contained in:
Sumner Evans
2019-08-02 22:36:59 -06:00
parent aa6f33083b
commit 9b6fa579a9
2 changed files with 48 additions and 17 deletions

View File

@@ -463,7 +463,6 @@ class CacheManager(metaclass=Singleton):
self.current_downloads = self.current_downloads.union( self.current_downloads = self.current_downloads.union(
set(song_ids)) set(song_ids))
for song_id in song_ids: for song_id in song_ids:
print(song_id)
self.download_limiter_semaphore.acquire() self.download_limiter_semaphore.acquire()
CacheManager.executor.submit(do_download_song, song_id) CacheManager.executor.submit(do_download_song, song_id)

View File

@@ -117,10 +117,14 @@ class ChromecastPlayer(Player):
chromecast = None chromecast = None
executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=50) executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=50)
def __init__(self, *args): class MediaStatusListener:
super().__init__(*args) on_new_media_status = None
self._timepos = None def new_media_status(self, status):
if self.on_new_media_status:
self.on_new_media_status(status)
media_status_listener = MediaStatusListener()
@classmethod @classmethod
def get_chromecasts(self) -> Future: def get_chromecasts(self) -> Future:
@@ -133,12 +137,32 @@ class ChromecastPlayer(Player):
@classmethod @classmethod
def set_playing_chromecast(self, chromecast): def set_playing_chromecast(self, chromecast):
self.chromecast = chromecast self.chromecast = chromecast
self.chromecast.media_controller.register_status_listener(
ChromecastPlayer.media_status_listener)
self.chromecast.wait() self.chromecast.wait()
print(f'Using: {chromecast.device.friendly_name}') print(f'Using: {chromecast.device.friendly_name}')
def __init__(self, *args):
super().__init__(*args)
self._timepos = None
self.time_incrementor_running = False
ChromecastPlayer.media_status_listener.on_new_media_status = self.on_new_media_status
def on_new_media_status(self, status):
# Detect the end of a track and go to the next one.
if (status.idle_reason == 'FINISHED' and status.player_state == 'IDLE'
and self._timepos > 0):
self.on_track_end()
self._timepos = status.current_time
def time_incrementor(self): def time_incrementor(self):
if self.time_incrementor_running:
return
self.time_incrementor_running = True
while True: while True:
if not self.playing: if not self.playing:
self.time_incrementor_running = False
raise Exception() raise Exception()
self._timepos += 0.5 self._timepos += 0.5
@@ -146,28 +170,33 @@ class ChromecastPlayer(Player):
sleep(0.5) sleep(0.5)
def start_time_incrementor(self): def start_time_incrementor(self):
def wait_for_playing(): ChromecastPlayer.executor.submit(self.time_incrementor)
while not self.playing:
print('waiting for playing') def wait_for_playing(self, callback, url=None):
def do_wait_for_playing():
while (not self.playing
or (url is not None and url !=
self.chromecast.media_controller.status.content_id)):
sleep(0.1) sleep(0.1)
ChromecastPlayer.executor.submit(self.time_incrementor) callback()
ChromecastPlayer.executor.submit(wait_for_playing) ChromecastPlayer.executor.submit(do_wait_for_playing)
def _is_playing(self): def _is_playing(self):
print('_is_playing',
self.chromecast.media_controller.status.player_is_playing)
return self.chromecast.media_controller.status.player_is_playing return self.chromecast.media_controller.status.player_is_playing
def play_media(self, file_or_url=None, progress=None): def play_media(self, file_or_url=None, progress=None):
self.chromecast.media_controller.play_media(file_or_url, 'audio/mp3') self.chromecast.media_controller.play_media(file_or_url, 'audio/mp3')
self.chromecast.media_controller.block_until_active()
self._timepos = 0 self._timepos = 0
self.start_time_incrementor()
self._song_loaded = True def on_play_begin():
if progress: self._song_loaded = True
self.seek(progress) if progress:
self.seek(progress)
self.start_time_incrementor()
self.wait_for_playing(on_play_begin, url=file_or_url)
def pause(self): def pause(self):
self.chromecast.media_controller.pause() self.chromecast.media_controller.pause()
@@ -177,10 +206,13 @@ class ChromecastPlayer(Player):
self.chromecast.media_controller.pause() self.chromecast.media_controller.pause()
else: else:
self.chromecast.media_controller.play() self.chromecast.media_controller.play()
self.wait_for_playing(self.start_time_incrementor)
def seek(self, value): def seek(self, value):
do_pause = not self.playing
self.chromecast.media_controller.seek(value) self.chromecast.media_controller.seek(value)
self._timepos = value if do_pause:
self.pause()
def _set_volume(self, value): def _set_volume(self, value):
# Chromecast volume is in the range [0, 1], not [0, 100]. # Chromecast volume is in the range [0, 1], not [0, 100].