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):
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,13 +51,14 @@ class GUICommunication:
self.update_progress()
def update_progress(self):
if self.app.player.playing and not self.track_progress_slider_dragged:
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
track_duration = self.app.player.playing_track.duration
track_duration = self.app.player.current_track.duration
progress = track_duration - remaining
@ -65,3 +66,8 @@ class GUICommunication:
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.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
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.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.music_channel.play(self.playing_track.sound)
self.track_progress.start()
self.app.gui_communication.on_track_start()
self.playing = True
self.paused = False
def track_finished(self):
# 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()
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.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
self.play()
self.track_progress.start()
self.paused = False
self.playing = True
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.current_sound_duration = self.current_track.duration
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.current_sound, self.current_sound_duration) = self.current_track.remaining(position)
self.play()
self.track_progress.start()
self.playing = True

View file

@ -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