From 7844e15aa2fcbb1c90e2bcfd33089781e032048c Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sun, 22 Dec 2024 17:41:54 +0100 Subject: [PATCH] OOPed everything a little more. --- wobuzz/gui_communication.py | 24 ++++--- wobuzz/player/player.py | 98 ++++++++++++++------------- wobuzz/player/track_progress_timer.py | 4 +- 3 files changed, 68 insertions(+), 58 deletions(-) diff --git a/wobuzz/gui_communication.py b/wobuzz/gui_communication.py index 58eeca3..f350bf1 100644 --- a/wobuzz/gui_communication.py +++ b/wobuzz/gui_communication.py @@ -39,7 +39,7 @@ class GUICommunication: def on_track_start(self): if self.app.player.playing: - duration = self.app.player.playing_track.duration + duration = self.app.player.current_track.duration self.track_control.track_progress_slider.setRange( 0, @@ -51,17 +51,23 @@ class GUICommunication: self.update_progress() def update_progress(self): - if self.app.player.playing and not self.track_progress_slider_dragged: - remaining = self.app.player.track_progress.timer.remainingTime() + if not self.track_progress_slider_dragged: + if self.app.player.playing: + remaining = self.app.player.track_progress.timer.remainingTime() - if remaining == -1: - remaining = self.app.player.track_progress.remaining_time + if remaining == -1: + remaining = self.app.player.track_progress.remaining_time - track_duration = self.app.player.playing_track.duration + track_duration = self.app.player.current_track.duration - progress = track_duration - remaining + progress = track_duration - remaining - self.track_control.progress_indicator.setText(self.app.utils.format_time(progress)) + self.track_control.progress_indicator.setText(self.app.utils.format_time(progress)) - self.track_control.track_progress_slider.setValue(progress) + self.track_control.track_progress_slider.setValue(progress) + + else: + self.track_control.progress_indicator.setText(self.app.utils.format_time(0)) + + self.track_control.track_progress_slider.setValue(0) diff --git a/wobuzz/player/player.py b/wobuzz/player/player.py index 5e632e1..c40344a 100644 --- a/wobuzz/player/player.py +++ b/wobuzz/player/player.py @@ -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() diff --git a/wobuzz/player/track_progress_timer.py b/wobuzz/player/track_progress_timer.py index bae790a..e11bffd 100644 --- a/wobuzz/player/track_progress_timer.py +++ b/wobuzz/player/track_progress_timer.py @@ -17,7 +17,7 @@ class TrackProgress: self.app.player.track_finished() def start(self): - self.timer.start(self.app.player.playing_segment_duration) + self.timer.start(self.app.player.current_sound_duration) def pause(self): self.remaining_time = self.timer.remainingTime() @@ -28,4 +28,4 @@ class TrackProgress: def stop(self): self.timer.stop() - self.remaining_time = self.app.player.playing_track.duration + self.remaining_time = self.app.player.current_track.duration