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,6 +59,7 @@ class Player:
self.stop() self.stop()
def start_playing(self): def start_playing(self):
if self.play_state.has_tracks:
self.music_channel.play(self.playing_track.sound) self.music_channel.play(self.playing_track.sound)
self.track_progress.start() self.track_progress.start()
self.play_state.paused = False self.play_state.paused = False
@ -74,6 +76,7 @@ class Player:
self.play_state.paused = False self.play_state.paused = False
def skip_current(self): def skip_current(self):
if self.play_state.has_tracks:
self.music_channel.stop() self.music_channel.stop()
self.track_progress.stop() self.track_progress.stop()
self.track_finished() self.track_finished()
@ -104,12 +107,14 @@ class Player:
self.start_playing() self.start_playing()
def stop(self): def stop(self):
if self.play_state.has_tracks:
self.music_channel.stop() self.music_channel.stop()
self.track_progress.stop() self.track_progress.stop()
self.play_state.playing = False self.play_state.playing = False
self.playing_segment_duration = self.playing_track.duration self.playing_segment_duration = self.playing_track.duration
def seek(self, position: int): def seek(self, position: int):
if self.play_state.has_tracks:
self.play_state.searched = True self.play_state.searched = True
self.music_channel.stop() self.music_channel.stop()
self.track_progress.stop() self.track_progress.stop()