#!/usr/bin/python3 import os import pygame.mixer from pydub import AudioSegment from pydub.effects import normalize import io from pygame import mixer from .track import Track class Player: def __init__(self, file_paths: list=[]): pygame.mixer.init() self.mixer = mixer self.mixer.music.set_endevent() self.playing = False self.paused = False if not file_paths: pass # 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 else: self.playing_track = None self.current_playlist_index = 0 def load_tracks(self, track_paths: list[str]): """ Load tracks from list of paths. """ tracks = [] for track_path in track_paths: if os.path.isfile(track_path): tracks.append(Track(track_path, True)) return tracks def track_finished(self): self.current_playlist_index += 1 self.playing_track = self.current_playlist[self.current_playlist_index] self.playing_track.sound.play() def skip_current(self): self.mixer.stop() self.track_finished() def previous_track(self): if self.current_playlist_index > 0: self.mixer.stop() self.current_playlist_index -= 1 self.playing_track = self.current_playlist[self.current_playlist_index] self.playing_track.sound.play() def toggle_playing(self): if self.playing and self.paused: self.mixer.pause() self.paused = False elif self.playing: self.mixer.unpause() self.paused = True else: self.playing_track.sound.play() self.paused = False self.playing = True def stop(self): self.mixer.stop() self.playing = False