import vlc import os import threading class MusicPlayer: def __init__(self, base_path="/home/tino/Desktop/Abschlussprojekt/test assistant/cloneAssistantAllInOne/RasPi_Voice_Assistant--WIP/musik/"): self.base_path = base_path self.instance = vlc.Instance() self.player = self.instance.media_player_new() self.lock = threading.Lock() self.files = [] self.current_index = 0 self.playing = False self.paused = False events = self.player.event_manager() events.event_attach( vlc.EventType.MediaPlayerEndReached, self._song_ended ) def load_genre(self, genre): folder = os.path.join(self.base_path, genre) if not os.path.isdir(folder): return False if self.playing or self.paused: self.player.stop() self.playing = False self.paused = False self.files = [ os.path.join(folder, f) for f in os.listdir(folder) if f.endswith((".mp3", ".wav")) ] self.current_index = 0 return bool(self.files) def _play_current(self): with self.lock: media = self.instance.media_new(self.files[self.current_index]) self.player.set_media(media) self.player.play() self.playing = True def _song_ended(self, event): self.next_song() def play(self): if not self.files: return False if self.paused: return self.resume() if not self.playing: self._play_current() return True def pause(self): if self.playing: self.player.pause() self.playing = False self.paused = True def resume(self): if self.paused: self.player.play() self.playing = True self.paused = False return True return False def stop(self): self.player.stop() self.playing = False self.paused = False self.current_index = 0 def next_song(self): with self.lock: self.current_index = (self.current_index + 1) % len(self.files) self._play_current() def previous_song(self): with self.lock: self.current_index = (self.current_index - 1) % len(self.files) self._play_current()