Added a "Playlist" class.

This commit is contained in:
The Wobbler 2024-12-24 17:22:30 +01:00
parent 1190059218
commit 94269fdae4
7 changed files with 119 additions and 53 deletions

View file

@ -5,6 +5,7 @@ import os
import pygame.mixer
import pygame.event
from .track import Track
from .playlist import Playlist
from .track_progress_timer import TrackProgress
@ -18,14 +19,11 @@ class Player:
self.track_progress = TrackProgress(self.app)
self.current_playlist = Playlist(self.app)
self.playing = False
self.paused = False
self.current_playlist = []
self.current_playlist_index = 0
self.current_track = None
self.current_sound = None
self.current_sound_duration = 0
@ -34,19 +32,11 @@ class Player:
Load tracks from list of paths.
"""
tracks = []
self.current_playlist = Playlist(self.app)
self.current_playlist.load_from_paths(track_paths)
for track_path in track_paths:
if os.path.isfile(track_path):
tracks.append(Track(track_path, True))
self.current_playlist = tracks
self.current_playlist_index = 0
self.current_track = self.current_playlist[0]
self.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
self.current_sound = self.current_playlist.current_track.sound
self.current_sound_duration = self.current_playlist.current_track.duration
def play(self):
self.music_channel.play(self.current_sound)
@ -55,14 +45,8 @@ class Player:
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
if not self.current_playlist.on_last_track():
self.current_sound, self.current_sound_duration = self.current_playlist.next_track()
self.play()
self.track_progress.start()
@ -72,8 +56,8 @@ class Player:
self.stop()
def start_playing(self):
self.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
self.current_sound = self.current_playlist.current_track.sound
self.current_sound_duration = self.current_playlist.current_track.duration
self.play()
self.track_progress.start()
@ -91,23 +75,19 @@ class Player:
self.paused = False
def next_track(self):
if self.current_playlist_index < len(self.current_playlist) - 1: # if the playing track isn't the last
if not self.current_playlist.on_last_track():
self.music_channel.stop()
self.track_progress.stop()
self.track_finished()
def previous_track(self):
if self.current_playlist_index > 0: # if the current track isn't the first in the playlist
if not self.current_playlist.on_first_track():
self.music_channel.stop()
self.current_playlist_index -= 1
self.current_track = self.current_playlist[self.current_playlist_index]
self.current_sound, self.current_sound_duration = self.current_playlist.previous_track()
self.track_progress.stop()
self.current_sound = self.current_track.sound
self.current_sound_duration = self.current_track.duration
self.play()
self.track_progress.start()
@ -116,7 +96,7 @@ class Player:
def stop(self):
self.music_channel.stop()
self.track_progress.stop()
self.current_sound_duration = self.current_track.duration
self.current_sound_duration = self.current_playlist.current_track.duration
self.playing = False
self.paused = False
@ -125,7 +105,7 @@ class Player:
self.music_channel.stop()
self.track_progress.stop()
(self.current_sound, self.current_sound_duration) = self.current_track.remaining(position)
(self.current_sound, self.current_sound_duration) = self.current_playlist.current_track.remaining(position)
self.play()
self.track_progress.start()