Wobuzz/wobuzz/player/player.py

220 lines
6.7 KiB
Python
Raw Normal View History

#!/usr/bin/python3
import time
import threading
import pygame.mixer
2024-12-21 19:00:06 +01:00
import pygame.event
2025-04-12 22:00:29 +02:00
2024-12-24 17:22:30 +01:00
from .playlist import Playlist
from .track_progress_timer import TrackProgress
2025-04-12 22:00:29 +02:00
from .mpris import MPRISServer
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.history = Playlist(self.app, "History")
self.current_playlist = None
2024-12-24 17:22:30 +01:00
2025-04-12 22:00:29 +02:00
self.mpris_server = MPRISServer(self.app)
# start mpris server in a thread (daemon = exit with main thread)
threading.Thread(target=self.mpris_server.start, daemon=True).start()
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.on_playstate_update()
2025-04-12 22:00:29 +02:00
self.mpris_server.exec_async(self.mpris_server.set_metadata(self.current_playlist.current_track.metadata))
# cache next track so it immediately starts when the current track finishes
self.cache_next_track()
def track_finished(self):
2024-12-24 17:22:30 +01:00
if not self.current_playlist.on_last_track():
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.history.append_track(self.current_playlist.current_track)
last_track = self.history.h_last_track()
self.app.gui.on_track_change(last_track, self.current_playlist.current_track)
if self.app.settings.clear_track_cache:
last_track.clear_cache()
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.history.append_track(self.current_playlist.current_track)
self.app.gui.on_track_change(self.history.h_last_track(), self.current_playlist.current_track)
self.app.gui.on_playstate_update()
2024-12-21 19:00:06 +01:00
def play_track_in_playlist(self, track_index):
self.stop()
if len(self.history.tracks) == 0:
self.history.append_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.history.append_track(self.current_playlist.current_track)
last_track = self.history.h_last_track()
self.app.gui.on_track_change(last_track, self.current_playlist.current_track)
if (
self.app.settings.clear_track_cache and
2025-02-27 17:58:32 +01:00
last_track is not None and
not last_track == self.current_playlist.current_track
):
last_track.clear_cache()
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.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.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()
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.history.append_track(self.current_playlist.current_track)
self.app.gui.on_track_change(self.history.h_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()
if self.current_playlist is not None and self.current_playlist.current_track is not None:
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.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
def caching_thread_function(self):
# cache the next track
2025-01-25 18:33:37 +01:00
# (usually ran in separate thread)
if self.current_playlist.on_last_track():
return
track = self.current_playlist.tracks[self.current_playlist.current_track_index + 1]
if not track.cached:
self.app.gui.on_background_job_start(
"Loading Track",
"Loading next track in the background so it starts immediately."
)
track.cache()
self.app.gui.on_background_job_stop("Loading Track")
def cache_next_track(self):
# function that creates a thread which will cache the next track
caching_thread = threading.Thread(target=self.caching_thread_function)
caching_thread.start()
2025-01-26 12:33:23 +01:00
def start_playlist(self, playlist):
self.stop()
if not playlist.loaded:
playlist.load()
while not playlist.has_tracks() and not playlist.loaded: # wait until first track is loaded
time.sleep(0.1)
if not playlist.has_tracks():
return
2025-01-26 12:33:23 +01:00
self.current_sound, self.current_sound_duration = playlist.set_track(0) # first track
self.current_playlist = playlist
self.start_playing()
2025-04-12 22:00:29 +02:00
def toggle_playing(self):
if self.playing and self.paused: # paused
self.unpause()
elif self.playing: # playing
self.pause()
# stopped but tracks in the current playlist
elif self.current_playlist is not None and self.current_playlist.has_tracks():
self.start_playing()
elif self.current_playlist is None:
if self.app.settings.latest_playlist is not None:
for playlist in self.app.library.playlists: # get loaded playlist by the path of the latest playlist
if playlist.path == self.app.settings.latest_playlist:
self.start_playlist(playlist)
break