Music Player: improve used feedback

This commit is contained in:
Thomas Farstrike
2025-10-28 20:14:55 +01:00
parent b2329aa975
commit cdd2af0eb1
2 changed files with 23 additions and 12 deletions
@@ -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()
@@ -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)