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

@ -39,7 +39,7 @@ class GUICommunication:
def on_track_start(self): def on_track_start(self):
if self.app.player.playing: 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( self.track_control.track_progress_slider.setRange(
0, 0,
@ -51,17 +51,23 @@ class GUICommunication:
self.update_progress() self.update_progress()
def update_progress(self): def update_progress(self):
if self.app.player.playing and not self.track_progress_slider_dragged: if not self.track_progress_slider_dragged:
remaining = self.app.player.track_progress.timer.remainingTime() if self.app.player.playing:
remaining = self.app.player.track_progress.timer.remainingTime()
if remaining == -1: if remaining == -1:
remaining = self.app.player.track_progress.remaining_time 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)

View file

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

View file

@ -17,7 +17,7 @@ class TrackProgress:
self.app.player.track_finished() self.app.player.track_finished()
def start(self): def start(self):
self.timer.start(self.app.player.playing_segment_duration) self.timer.start(self.app.player.current_sound_duration)
def pause(self): def pause(self):
self.remaining_time = self.timer.remainingTime() self.remaining_time = self.timer.remainingTime()
@ -28,4 +28,4 @@ class TrackProgress:
def stop(self): def stop(self):
self.timer.stop() self.timer.stop()
self.remaining_time = self.app.player.playing_track.duration self.remaining_time = self.app.player.current_track.duration