From cdd2af0eb1cbd8f4ee94c048228d3e889bbbbe82 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Tue, 28 Oct 2025 20:14:55 +0100 Subject: [PATCH] Music Player: improve used feedback --- .../assets/audio_player.py | 16 ++++++++++------ .../assets/music_player.py | 19 +++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py index 192bbd05..9b9b287e 100644 --- a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py +++ b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py @@ -11,7 +11,7 @@ import micropython # ---------------------------------------------------------------------- class AudioPlayer: _i2s = None - _volume = 50 # 0-100 + _volume = 50 # 0-100 _keep_running = True # ------------------------------------------------------------------ @@ -22,10 +22,10 @@ class AudioPlayer: """Return (data_start, data_size, sample_rate, channels, bits_per_sample)""" f.seek(0) if f.read(4) != b'RIFF': - raise ValueError("Not a RIFF file") + raise ValueError("Not a RIFF (standard .wav) file") file_size = int.from_bytes(f.read(4), 'little') + 8 if f.read(4) != b'WAVE': - raise ValueError("Not a WAVE file") + raise ValueError("Not a WAVE (standard .wav) file") pos = 12 sample_rate = None @@ -154,7 +154,7 @@ class AudioPlayer: # Main playback routine # ------------------------------------------------------------------ @classmethod - def play_wav(cls, filename): + def play_wav(cls, filename, result_callback=None): cls._keep_running = True try: with open(filename, 'rb') as f: @@ -265,9 +265,13 @@ class AudioPlayer: total_original += to_read - print("Playback finished.") + print(f"Finished playing {filename}") + if result_callback: + result_callback(f"Finished playing {filename}") except Exception as e: - print(f"AudioPlayer error: {e}") + print(f"Error: {e}\nwhile playing {filename}") + if result_callback: + result_callback(f"Error: {e}\nwhile playing {filename}") finally: if cls._i2s: cls._i2s.deinit() diff --git a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py index 9a23595f..c926a8b0 100644 --- a/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py +++ b/internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py @@ -39,11 +39,11 @@ class MusicPlayer(Activity): file = self.file_explorer.explorer_get_selected_file_name() fullpath = f"{clean_path}{file}" print(f"Selected: {fullpath}") - if fullpath.lower().endswith('.wav'): - self.destination = FullscreenPlayer - self.startActivity(Intent(activity_class=FullscreenPlayer).putExtra("filename", fullpath)) - else: - print("INFO: ignoring unsupported file format") + #if fullpath.lower().endswith('.wav'): + self.destination = FullscreenPlayer + self.startActivity(Intent(activity_class=FullscreenPlayer).putExtra("filename", fullpath)) + #else: + # print("INFO: ignoring unsupported file format") class FullscreenPlayer(Activity): # No __init__() so super.__init__() will be called automatically @@ -101,7 +101,7 @@ class FullscreenPlayer(Activity): AudioPlayer.stop_playing() time.sleep(0.1) _thread.stack_size(mpos.apps.good_stack_size()) - _thread.start_new_thread(AudioPlayer.play_wav, (self._filename,)) + _thread.start_new_thread(AudioPlayer.play_wav, (self._filename,self.player_finished,)) def focus_obj(self, obj): obj.set_style_border_color(lv.theme_get_color_primary(None),lv.PART.MAIN) @@ -113,3 +113,10 @@ class FullscreenPlayer(Activity): def stop_button_clicked(self, event): AudioPlayer.stop_playing() self.finish() + + def player_finished(self, result=None): + text = f"Finished playing {self._filename}" + if result: + text = result + print(f"AudioPlayer finished: {text}") + lv.async_call(lambda l: self._filename_label.set_text(text), None)