Wobuzz/wobuzz/player/player.py

128 lines
3.6 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
import os
import pygame.mixer
2024-12-21 19:00:06 +01:00
import pygame.event
from .track import Track
2024-12-24 17:22:30 +01:00
from .playlist import Playlist
from .track_progress_timer import TrackProgress
class Player:
2024-12-22 16:11:43 +01:00
def __init__(self, app):
2024-12-21 19:00:06 +01:00
self.app = app
pygame.mixer.init()
2024-12-21 19:00:06 +01:00
self.mixer = pygame.mixer
self.music_channel = self.mixer.Channel(0)
self.track_progress = TrackProgress(self.app)
self.current_playlist = Playlist(self.app, "None")
2024-12-24 17:22:30 +01:00
self.playing = False
self.paused = False
2024-12-22 17:41:54 +01:00
self.current_sound = None
self.current_sound_duration = 0
2024-12-22 17:41:54 +01:00
def play(self):
self.music_channel.play(self.current_sound)
self.playing = True
self.paused = False
self.app.gui_communication.on_playstate_update()
def track_finished(self):
2024-12-24 17:22:30 +01:00
if not self.current_playlist.on_last_track():
previous_track = self.current_playlist.current_track
2024-12-24 17:22:30 +01:00
self.current_sound, self.current_sound_duration = self.current_playlist.next_track()
2024-12-22 17:41:54 +01:00
self.play()
self.track_progress.start()
self.app.gui_communication.on_track_change(previous_track, self.current_playlist.current_track)
else:
self.stop()
2024-12-21 19:00:06 +01:00
def start_playing(self):
2024-12-24 17:22:30 +01:00
self.current_sound = self.current_playlist.current_track.sound
self.current_sound_duration = self.current_playlist.current_track.duration
2024-12-22 17:41:54 +01:00
self.play()
self.track_progress.start()
self.app.gui_communication.on_track_change(None, self.current_playlist.current_track)
self.app.gui_communication.on_playstate_update()
2024-12-21 19:00:06 +01:00
def play_track_in_playlist(self, track_index):
self.stop()
previous_track = self.current_playlist.current_track
self.current_sound, self.current_sound_duration = self.current_playlist.set_track(track_index)
self.play()
self.track_progress.start()
self.app.gui_communication.on_track_change(previous_track, self.current_playlist.current_track)
2024-12-21 19:00:06 +01:00
def pause(self):
self.music_channel.pause()
self.track_progress.pause()
self.paused = True
2024-12-21 19:00:06 +01:00
self.app.gui_communication.on_playstate_update()
2024-12-21 19:00:06 +01:00
def unpause(self):
self.music_channel.unpause()
self.track_progress.unpause()
2024-12-22 17:41:54 +01:00
2024-12-22 17:20:18 +01:00
self.playing = True
2024-12-22 17:41:54 +01:00
self.paused = False
self.app.gui_communication.on_playstate_update()
2024-12-22 17:20:18 +01:00
def next_track(self):
2024-12-24 17:22:30 +01:00
if not self.current_playlist.on_last_track():
self.music_channel.stop()
self.track_progress.stop()
self.track_finished()
def previous_track(self):
2024-12-24 17:22:30 +01:00
if not self.current_playlist.on_first_track():
2024-12-21 19:00:06 +01:00
self.music_channel.stop()
last_track = self.current_playlist.current_track
2024-12-24 17:22:30 +01:00
self.current_sound, self.current_sound_duration = self.current_playlist.previous_track()
2024-12-22 17:41:54 +01:00
self.track_progress.stop()
2024-12-22 17:41:54 +01:00
self.play()
self.track_progress.start()
self.app.gui_communication.on_track_change(last_track, self.current_playlist.current_track)
2024-12-22 17:20:18 +01:00
def stop(self):
2024-12-22 17:41:54 +01:00
self.music_channel.stop()
self.track_progress.stop()
2024-12-24 17:22:30 +01:00
self.current_sound_duration = self.current_playlist.current_track.duration
2024-12-22 17:41:54 +01:00
self.playing = False
self.paused = False
2024-12-22 17:20:18 +01:00
self.app.gui_communication.on_playstate_update()
2024-12-21 20:20:06 +01:00
def seek(self, position: int):
2024-12-22 17:41:54 +01:00
self.music_channel.stop()
self.track_progress.stop()
2024-12-24 17:22:30 +01:00
(self.current_sound, self.current_sound_duration) = self.current_playlist.current_track.remaining(position)
2024-12-21 20:20:06 +01:00
2024-12-22 17:41:54 +01:00
self.play()
self.track_progress.start()
2024-12-22 17:20:18 +01:00