OOPed everything a little more.

This commit is contained in:
The Wobbler 2024-12-22 17:41:54 +01:00
parent 19563930c5
commit 7844e15aa2
3 changed files with 68 additions and 58 deletions

View file

@ -20,14 +20,14 @@ class Player:
self.playing = False
self.paused = False
self.has_tracks = False
self.searched = False
self.current_playlist = []
self.playing_track = None
self.current_playlist_index = 0
self.playing_segment = None
self.playing_segment_duration = 0
self.current_track = None
self.current_sound = None
self.current_sound_duration = 0
def load_tracks_from_paths(self, track_paths: list[str]):
"""
@ -41,35 +41,42 @@ class Player:
tracks.append(Track(track_path, True))
self.current_playlist = tracks
self.playing_track = self.current_playlist[0]
self.current_playlist_index = 0
self.playing_segment = self.playing_track.sound
self.playing_segment_duration = self.playing_track.duration
self.has_tracks = len(self.current_playlist) > 0
self.current_track = self.current_playlist[0]
self.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
def play(self):
self.music_channel.play(self.current_sound)
self.playing = True
self.paused = False
def track_finished(self):
self.current_playlist_index += 1
if self.current_playlist_index < len(self.current_playlist):
self.playing_track = self.current_playlist[self.current_playlist_index]
self.playing_segment = self.playing_track.sound
self.playing_segment_duration = self.playing_track.duration
self.music_channel.play(self.playing_track.sound)
# if the last track wasn't the last in the playlist
if self.current_playlist_index < len(self.current_playlist) - 1:
self.current_playlist_index += 1
self.current_track = self.current_playlist[self.current_playlist_index]
self.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
self.play()
self.track_progress.start()
self.app.gui_communication.on_track_start()
self.playing = True
self.paused = False
else:
self.current_playlist_index -= 1
self.stop()
def start_playing(self):
if self.has_tracks:
self.music_channel.play(self.playing_track.sound)
self.track_progress.start()
self.paused = False
self.playing = True
self.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
self.play()
self.track_progress.start()
def pause(self):
self.music_channel.pause()
@ -79,32 +86,32 @@ class Player:
def unpause(self):
self.music_channel.unpause()
self.track_progress.unpause()
self.paused = False
self.playing = True
self.paused = False
def next_track(self):
if self.has_tracks and self.current_playlist_index < len(self.current_playlist) - 1:
if self.current_playlist_index < len(self.current_playlist) - 1: # if the playing track isn't the last
self.music_channel.stop()
self.track_progress.stop()
self.track_finished()
def previous_track(self):
if self.current_playlist_index > 0:
if self.current_playlist_index > 0: # if the current track isn't the first in the playlist
self.music_channel.stop()
self.current_playlist_index -= 1
self.playing_track = self.current_playlist[self.current_playlist_index]
self.current_track = self.current_playlist[self.current_playlist_index]
self.track_progress.stop()
self.playing_segment = self.playing_track.sound
self.playing_segment_duration = self.playing_track.duration
self.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
self.music_channel.play(self.playing_track.sound)
self.play()
self.track_progress.start()
self.app.gui_communication.on_track_start()
self.playing = True
self.paused = False
self.app.gui_communication.on_track_start()
def toggle_playing(self):
if self.playing and self.paused:
@ -117,22 +124,19 @@ class Player:
self.start_playing()
def stop(self):
if self.has_tracks:
self.music_channel.stop()
self.track_progress.stop()
self.playing_segment_duration = self.playing_track.duration
self.music_channel.stop()
self.track_progress.stop()
self.current_sound_duration = self.current_track.duration
self.playing = False
self.paused = False
self.playing = False
self.paused = False
def seek(self, position: int):
if self.has_tracks:
self.searched = True
self.music_channel.stop()
self.track_progress.stop()
(self.playing_segment, self.playing_segment_duration) = self.playing_track.remaining(position)
self.music_channel.play(self.playing_segment)
self.track_progress.start()
self.music_channel.stop()
self.track_progress.stop()
self.playing = True
(self.current_sound, self.current_sound_duration) = self.current_track.remaining(position)
self.play()
self.track_progress.start()