Really janky logic for fixing song queue loading

This commit is contained in:
Sumner Evans
2019-08-03 00:28:28 -06:00
parent 75bd0bf79f
commit 0e8b41d049
2 changed files with 32 additions and 16 deletions

View File

@@ -53,7 +53,7 @@ class LibremsonicApp(Gtk.Application):
self.mpv_player = MPVPlayer(time_observer, on_track_end)
self.chromecast_player = ChromecastPlayer(time_observer, on_track_end)
self.player = self.chromecast_player
self.player = self.mpv_player
# Handle command line option parsing.
def do_command_line(self, command_line):
@@ -170,6 +170,7 @@ class LibremsonicApp(Gtk.Application):
self.play_song(self.state.play_queue[current_idx + 1], reset=True)
def on_prev_track(self, action, params):
# TODO there is a bug where you can't go back multiple songs fast
current_idx = self.state.play_queue.index(self.state.current_song.id)
# Go back to the beginning of the song if we are past 5 seconds.
# Otherwise, go to the previous song.
@@ -307,6 +308,9 @@ class LibremsonicApp(Gtk.Application):
def update_play_state_from_server(self):
# TODO make this non-blocking eventually (need to make everything in
# loading state)
self.player.pause()
self.state.playing = False
play_queue = CacheManager.get_play_queue()
self.state.play_queue = [s.id for s in play_queue.entry]
self.state.song_progress = play_queue.position / 1000
@@ -314,6 +318,8 @@ class LibremsonicApp(Gtk.Application):
current_song_idx = self.state.play_queue.index(str(play_queue.current))
self.state.current_song = play_queue.entry[current_song_idx]
self.player.reset()
self.update_window()
def play_song(
@@ -329,6 +335,7 @@ class LibremsonicApp(Gtk.Application):
# TODO force mp3 while getting chromecast working.
uri, stream = CacheManager.get_song_filename_or_stream(
song,
force_stream=(self.player == self.chromecast_player),
format='mp3',
)
@@ -339,6 +346,7 @@ class LibremsonicApp(Gtk.Application):
# Prevent it from doing the thing where it continually loads
# songs when it has to download.
if reset:
self.player.reset()
self.state.song_progress = 0
# If streaming, also download the song.

View File

@@ -37,6 +37,10 @@ class Player:
def volume(self, value):
return self._set_volume(value)
def reset(self):
raise NotImplementedError(
'reset must be implemented by implementor of Player')
def play_media(self, file_or_url=None, progress=None):
raise NotImplementedError(
'play_media must be implemented by implementor of Player')
@@ -75,23 +79,33 @@ class MPVPlayer(Player):
super().__init__(*args)
self.mpv = mpv.MPV()
self.had_progress_value = False
self.progress_value_lock = threading.Lock()
self.progress_value_count = 0
@self.mpv.property_observer('time-pos')
def time_observer(_name, value):
self.on_timepos_change(value)
if value is None and self.had_progress_value:
if value is None and self.progress_value_count > 1:
self.on_track_end()
self.had_progress_value = False
with self.progress_value_lock:
self.progress_value_count = 0
if value:
self.had_progress_value = True
with self.progress_value_lock:
self.progress_value_count += 1
def _is_playing(self):
return not self.mpv.pause
def reset(self):
self._song_loaded = False
with self.progress_value_lock:
self.progress_value_count = 0
def play_media(self, file_or_url, progress=None):
self.had_progress_value = False
with self.progress_value_lock:
self.progress_value_count = 0
self.mpv.pause = False
self.mpv.command(
'loadfile',
@@ -226,6 +240,9 @@ class ChromecastPlayer(Player):
def _is_playing(self):
return self.chromecast.media_controller.status.player_is_playing
def reset(self):
self._song_loaded = False
def play_media(self, file_or_url=None, progress=None):
stream_scheme = urlparse(file_or_url).scheme
if not stream_scheme:
@@ -234,16 +251,7 @@ class ChromecastPlayer(Player):
file_or_url = file_or_url[strlen:]
file_or_url = f'http://{self.host_ip}:8080/{quote(file_or_url)}'
print(
file_or_url,
'audio/mp3'
if stream_scheme else mimetypes.guess_type(file_or_url)[0],
)
self.chromecast.media_controller.play_media(
file_or_url,
'audio/m4a'
)
self.chromecast.media_controller.play_media(file_or_url, 'audio/mp3')
def on_play_begin():
self._song_loaded = True