Fixed some "crashes" that occurred on track control button presses when the current_playlist variable was empty.

This commit is contained in:
The Wobbler 2024-12-22 16:18:03 +01:00
parent d3a6cb7dd8
commit fac2dc0f1b
2 changed files with 23 additions and 17 deletions

View file

@ -5,6 +5,7 @@ from dataclasses import dataclass
@dataclass @dataclass
class PlayState: class PlayState:
has_tracks: bool=False
playing: bool=False playing: bool=False
paused: bool=False paused: bool=False
searched: bool=False searched: bool=False

View file

@ -41,6 +41,7 @@ class Player:
self.current_playlist_index = 0 self.current_playlist_index = 0
self.playing_segment = self.playing_track.sound self.playing_segment = self.playing_track.sound
self.playing_segment_duration = self.playing_track.duration self.playing_segment_duration = self.playing_track.duration
self.play_state.has_tracks = len(self.current_playlist) > 0
def track_finished(self): def track_finished(self):
self.current_playlist_index += 1 self.current_playlist_index += 1
@ -58,10 +59,11 @@ class Player:
self.stop() self.stop()
def start_playing(self): def start_playing(self):
self.music_channel.play(self.playing_track.sound) if self.play_state.has_tracks:
self.track_progress.start() self.music_channel.play(self.playing_track.sound)
self.play_state.paused = False self.track_progress.start()
self.play_state.playing = True self.play_state.paused = False
self.play_state.playing = True
def pause(self): def pause(self):
self.music_channel.pause() self.music_channel.pause()
@ -74,9 +76,10 @@ class Player:
self.play_state.paused = False self.play_state.paused = False
def skip_current(self): def skip_current(self):
self.music_channel.stop() if self.play_state.has_tracks:
self.track_progress.stop() self.music_channel.stop()
self.track_finished() self.track_progress.stop()
self.track_finished()
def previous_track(self): def previous_track(self):
if self.current_playlist_index > 0: if self.current_playlist_index > 0:
@ -104,16 +107,18 @@ class Player:
self.start_playing() self.start_playing()
def stop(self): def stop(self):
self.music_channel.stop() if self.play_state.has_tracks:
self.track_progress.stop() self.music_channel.stop()
self.play_state.playing = False self.track_progress.stop()
self.playing_segment_duration = self.playing_track.duration self.play_state.playing = False
self.playing_segment_duration = self.playing_track.duration
def seek(self, position: int): def seek(self, position: int):
self.play_state.searched = True if self.play_state.has_tracks:
self.music_channel.stop() self.play_state.searched = True
self.track_progress.stop() self.music_channel.stop()
(self.playing_segment, self.playing_segment_duration) = self.playing_track.remaining(position) self.track_progress.stop()
self.music_channel.play(self.playing_segment) (self.playing_segment, self.playing_segment_duration) = self.playing_track.remaining(position)
self.track_progress.start() self.music_channel.play(self.playing_segment)
self.track_progress.start()