Skip to content

Commit cdd2af0

Browse files
Music Player: improve used feedback
1 parent b2329aa commit cdd2af0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# ----------------------------------------------------------------------
1212
class AudioPlayer:
1313
_i2s = None
14-
_volume = 50 # 0-100
14+
_volume = 50 # 0-100
1515
_keep_running = True
1616

1717
# ------------------------------------------------------------------
@@ -22,10 +22,10 @@ def find_data_chunk(f):
2222
"""Return (data_start, data_size, sample_rate, channels, bits_per_sample)"""
2323
f.seek(0)
2424
if f.read(4) != b'RIFF':
25-
raise ValueError("Not a RIFF file")
25+
raise ValueError("Not a RIFF (standard .wav) file")
2626
file_size = int.from_bytes(f.read(4), 'little') + 8
2727
if f.read(4) != b'WAVE':
28-
raise ValueError("Not a WAVE file")
28+
raise ValueError("Not a WAVE (standard .wav) file")
2929

3030
pos = 12
3131
sample_rate = None
@@ -154,7 +154,7 @@ def _convert_32_to_16(buf: bytearray) -> bytearray:
154154
# Main playback routine
155155
# ------------------------------------------------------------------
156156
@classmethod
157-
def play_wav(cls, filename):
157+
def play_wav(cls, filename, result_callback=None):
158158
cls._keep_running = True
159159
try:
160160
with open(filename, 'rb') as f:
@@ -265,9 +265,13 @@ def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
265265

266266
total_original += to_read
267267

268-
print("Playback finished.")
268+
print(f"Finished playing {filename}")
269+
if result_callback:
270+
result_callback(f"Finished playing {filename}")
269271
except Exception as e:
270-
print(f"AudioPlayer error: {e}")
272+
print(f"Error: {e}\nwhile playing {filename}")
273+
if result_callback:
274+
result_callback(f"Error: {e}\nwhile playing {filename}")
271275
finally:
272276
if cls._i2s:
273277
cls._i2s.deinit()

internal_filesystem/apps/com.micropythonos.musicplayer/assets/music_player.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ def file_explorer_event_cb(self, event):
3939
file = self.file_explorer.explorer_get_selected_file_name()
4040
fullpath = f"{clean_path}{file}"
4141
print(f"Selected: {fullpath}")
42-
if fullpath.lower().endswith('.wav'):
43-
self.destination = FullscreenPlayer
44-
self.startActivity(Intent(activity_class=FullscreenPlayer).putExtra("filename", fullpath))
45-
else:
46-
print("INFO: ignoring unsupported file format")
42+
#if fullpath.lower().endswith('.wav'):
43+
self.destination = FullscreenPlayer
44+
self.startActivity(Intent(activity_class=FullscreenPlayer).putExtra("filename", fullpath))
45+
#else:
46+
# print("INFO: ignoring unsupported file format")
4747

4848
class FullscreenPlayer(Activity):
4949
# No __init__() so super.__init__() will be called automatically
@@ -101,7 +101,7 @@ def onResume(self, screen):
101101
AudioPlayer.stop_playing()
102102
time.sleep(0.1)
103103
_thread.stack_size(mpos.apps.good_stack_size())
104-
_thread.start_new_thread(AudioPlayer.play_wav, (self._filename,))
104+
_thread.start_new_thread(AudioPlayer.play_wav, (self._filename,self.player_finished,))
105105

106106
def focus_obj(self, obj):
107107
obj.set_style_border_color(lv.theme_get_color_primary(None),lv.PART.MAIN)
@@ -113,3 +113,10 @@ def defocus_obj(self, obj):
113113
def stop_button_clicked(self, event):
114114
AudioPlayer.stop_playing()
115115
self.finish()
116+
117+
def player_finished(self, result=None):
118+
text = f"Finished playing {self._filename}"
119+
if result:
120+
text = result
121+
print(f"AudioPlayer finished: {text}")
122+
lv.async_call(lambda l: self._filename_label.set_text(text), None)

0 commit comments

Comments
 (0)