OOPed everything a little.

This commit is contained in:
The Wobbler 2024-12-22 16:11:43 +01:00
parent 3dfb07ab14
commit d3a6cb7dd8
4 changed files with 40 additions and 37 deletions

View file

@ -38,7 +38,7 @@ class GUICommunication:
self.app.player.seek(self.track_control.track_progress_slider.value())
def on_track_start(self):
if self.app.player.playing:
if self.app.player.play_state.playing:
duration = self.app.player.playing_track.duration
self.track_control.track_progress_slider.setRange(
@ -51,7 +51,7 @@ class GUICommunication:
self.update_progress()
def update_progress(self):
if self.app.player.playing and not self.track_progress_slider_dragged:
if self.app.player.play_state.playing and not self.track_progress_slider_dragged:
remaining = self.app.player.track_progress.timer.remainingTime()
if remaining == -1:

View file

@ -13,11 +13,15 @@ class Wobuzz:
self.qt_app = QApplication([])
self.utils = Utils(self)
self.player = Player(self, sys.argv[1:])
self.player = Player(self)
self.gui = GUI(self)
self.gui_communication = GUICommunication(self)
track_paths = sys.argv[1:]
if track_paths:
self.player.load_tracks_from_paths(track_paths)
if __name__ == "__main__":
wobuzz = Wobuzz()

View file

@ -0,0 +1,10 @@
#!/usr/bin/python3
from dataclasses import dataclass
@dataclass
class PlayState:
playing: bool=False
paused: bool=False
searched: bool=False

View file

@ -6,41 +6,26 @@ import pygame.mixer
import pygame.event
from .track import Track
from .track_progress_timer import TrackProgress
from .play_state import PlayState
class Player:
def __init__(self, app, file_paths: list=[]):
def __init__(self, app):
self.app = app
self.track_progress = TrackProgress(self.app)
self.play_state = PlayState()
pygame.mixer.init()
self.mixer = pygame.mixer
self.music_channel = self.mixer.Channel(0)
self.playing = False
self.paused = 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
if not file_paths:
self.current_playlist = []
# loading of last opened files will be implemented in the future
else:
self.current_playlist = self.load_tracks(file_paths)
if self.current_playlist:
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
else:
self.playing_track = None
self.current_playlist_index = 0
self.playing_segment = None
self.playing_segment_duration = 0
def load_tracks(self, track_paths: list[str]):
def load_tracks_from_paths(self, track_paths: list[str]):
"""
Load tracks from list of paths.
"""
@ -51,7 +36,11 @@ class Player:
if os.path.isfile(track_path):
tracks.append(Track(track_path, True))
return tracks
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
def track_finished(self):
self.current_playlist_index += 1
@ -71,18 +60,18 @@ class Player:
def start_playing(self):
self.music_channel.play(self.playing_track.sound)
self.track_progress.start()
self.paused = False
self.playing = True
self.play_state.paused = False
self.play_state.playing = True
def pause(self):
self.music_channel.pause()
self.track_progress.pause()
self.paused = True
self.play_state.paused = True
def unpause(self):
self.music_channel.unpause()
self.track_progress.unpause()
self.paused = False
self.play_state.paused = False
def skip_current(self):
self.music_channel.stop()
@ -105,10 +94,10 @@ class Player:
self.app.gui_communication.on_track_start()
def toggle_playing(self):
if self.playing and self.paused:
if self.play_state.playing and self.play_state.paused:
self.unpause()
elif self.playing:
elif self.play_state.playing:
self.pause()
else:
@ -117,11 +106,11 @@ class Player:
def stop(self):
self.music_channel.stop()
self.track_progress.stop()
self.playing = False
self.play_state.playing = False
self.playing_segment_duration = self.playing_track.duration
def seek(self, position: int):
self.searched = True
self.play_state.searched = True
self.music_channel.stop()
self.track_progress.stop()
(self.playing_segment, self.playing_segment_duration) = self.playing_track.remaining(position)