Really janky logic for fixing song queue loading
This commit is contained in:
@@ -53,7 +53,7 @@ class LibremsonicApp(Gtk.Application):
|
|||||||
|
|
||||||
self.mpv_player = MPVPlayer(time_observer, on_track_end)
|
self.mpv_player = MPVPlayer(time_observer, on_track_end)
|
||||||
self.chromecast_player = ChromecastPlayer(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.
|
# Handle command line option parsing.
|
||||||
def do_command_line(self, command_line):
|
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)
|
self.play_song(self.state.play_queue[current_idx + 1], reset=True)
|
||||||
|
|
||||||
def on_prev_track(self, action, params):
|
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)
|
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.
|
# Go back to the beginning of the song if we are past 5 seconds.
|
||||||
# Otherwise, go to the previous song.
|
# Otherwise, go to the previous song.
|
||||||
@@ -307,6 +308,9 @@ class LibremsonicApp(Gtk.Application):
|
|||||||
def update_play_state_from_server(self):
|
def update_play_state_from_server(self):
|
||||||
# TODO make this non-blocking eventually (need to make everything in
|
# TODO make this non-blocking eventually (need to make everything in
|
||||||
# loading state)
|
# loading state)
|
||||||
|
self.player.pause()
|
||||||
|
self.state.playing = False
|
||||||
|
|
||||||
play_queue = CacheManager.get_play_queue()
|
play_queue = CacheManager.get_play_queue()
|
||||||
self.state.play_queue = [s.id for s in play_queue.entry]
|
self.state.play_queue = [s.id for s in play_queue.entry]
|
||||||
self.state.song_progress = play_queue.position / 1000
|
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))
|
current_song_idx = self.state.play_queue.index(str(play_queue.current))
|
||||||
self.state.current_song = play_queue.entry[current_song_idx]
|
self.state.current_song = play_queue.entry[current_song_idx]
|
||||||
|
|
||||||
|
self.player.reset()
|
||||||
|
|
||||||
self.update_window()
|
self.update_window()
|
||||||
|
|
||||||
def play_song(
|
def play_song(
|
||||||
@@ -329,6 +335,7 @@ class LibremsonicApp(Gtk.Application):
|
|||||||
# TODO force mp3 while getting chromecast working.
|
# TODO force mp3 while getting chromecast working.
|
||||||
uri, stream = CacheManager.get_song_filename_or_stream(
|
uri, stream = CacheManager.get_song_filename_or_stream(
|
||||||
song,
|
song,
|
||||||
|
force_stream=(self.player == self.chromecast_player),
|
||||||
format='mp3',
|
format='mp3',
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -339,6 +346,7 @@ class LibremsonicApp(Gtk.Application):
|
|||||||
# Prevent it from doing the thing where it continually loads
|
# Prevent it from doing the thing where it continually loads
|
||||||
# songs when it has to download.
|
# songs when it has to download.
|
||||||
if reset:
|
if reset:
|
||||||
|
self.player.reset()
|
||||||
self.state.song_progress = 0
|
self.state.song_progress = 0
|
||||||
|
|
||||||
# If streaming, also download the song.
|
# If streaming, also download the song.
|
||||||
|
@@ -37,6 +37,10 @@ class Player:
|
|||||||
def volume(self, value):
|
def volume(self, value):
|
||||||
return self._set_volume(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):
|
def play_media(self, file_or_url=None, progress=None):
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
'play_media must be implemented by implementor of Player')
|
'play_media must be implemented by implementor of Player')
|
||||||
@@ -75,23 +79,33 @@ class MPVPlayer(Player):
|
|||||||
super().__init__(*args)
|
super().__init__(*args)
|
||||||
|
|
||||||
self.mpv = mpv.MPV()
|
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')
|
@self.mpv.property_observer('time-pos')
|
||||||
def time_observer(_name, value):
|
def time_observer(_name, value):
|
||||||
self.on_timepos_change(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.on_track_end()
|
||||||
self.had_progress_value = False
|
with self.progress_value_lock:
|
||||||
|
self.progress_value_count = 0
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
self.had_progress_value = True
|
with self.progress_value_lock:
|
||||||
|
self.progress_value_count += 1
|
||||||
|
|
||||||
def _is_playing(self):
|
def _is_playing(self):
|
||||||
return not self.mpv.pause
|
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):
|
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.pause = False
|
||||||
self.mpv.command(
|
self.mpv.command(
|
||||||
'loadfile',
|
'loadfile',
|
||||||
@@ -226,6 +240,9 @@ class ChromecastPlayer(Player):
|
|||||||
def _is_playing(self):
|
def _is_playing(self):
|
||||||
return self.chromecast.media_controller.status.player_is_playing
|
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):
|
def play_media(self, file_or_url=None, progress=None):
|
||||||
stream_scheme = urlparse(file_or_url).scheme
|
stream_scheme = urlparse(file_or_url).scheme
|
||||||
if not stream_scheme:
|
if not stream_scheme:
|
||||||
@@ -234,16 +251,7 @@ class ChromecastPlayer(Player):
|
|||||||
file_or_url = file_or_url[strlen:]
|
file_or_url = file_or_url[strlen:]
|
||||||
file_or_url = f'http://{self.host_ip}:8080/{quote(file_or_url)}'
|
file_or_url = f'http://{self.host_ip}:8080/{quote(file_or_url)}'
|
||||||
|
|
||||||
print(
|
self.chromecast.media_controller.play_media(file_or_url, 'audio/mp3')
|
||||||
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'
|
|
||||||
)
|
|
||||||
|
|
||||||
def on_play_begin():
|
def on_play_begin():
|
||||||
self._song_loaded = True
|
self._song_loaded = True
|
||||||
|
Reference in New Issue
Block a user