Made the player always cache also the next track when Player.play() gets executed so that there is completely no delay when the track finished and a new track starts.

This commit is contained in:
The Wobbler 2025-01-25 16:17:42 +01:00
parent 6e99c85f88
commit 88b846f3b6

View file

@ -1,5 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
import threading
import pygame.mixer import pygame.mixer
import pygame.event import pygame.event
from .playlist import Playlist from .playlist import Playlist
@ -32,6 +33,10 @@ class Player:
self.app.gui.track_control.on_playstate_update() self.app.gui.track_control.on_playstate_update()
# cache next track so it immediately starts when the current track finishes
caching_thread = threading.Thread(target=self.cache_next_track)
caching_thread.start()
def track_finished(self): def track_finished(self):
if not self.current_playlist.on_last_track(): if not self.current_playlist.on_last_track():
previous_track = self.current_playlist.current_track previous_track = self.current_playlist.current_track
@ -122,3 +127,14 @@ class Player:
self.play() self.play()
self.track_progress.start() self.track_progress.start()
def cache_next_track(self):
# cache the next track
# (usually run in separate thread)
if self.current_playlist.on_last_track():
return
track = self.current_playlist.tracks[self.current_playlist.current_track_index + 1]
if not track.cached:
track.cache()