Skip to content

Commit 219b80f

Browse files
Simple stopping method works
1 parent c153bbe commit 219b80f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class AudioPlayer:
1111
# class-level defaults (shared by every instance)
1212
_i2s = None # the I2S object (created once per playback)
1313
_volume = 50 # 0-100 (100 = full scale)
14+
_keep_running = True
1415

1516
@staticmethod
1617
def find_data_chunk(f):
@@ -67,11 +68,17 @@ def get_volume(cls) -> int:
6768
"""Return current volume 0-100."""
6869
return cls._volume
6970

71+
#@classmethod
72+
def stop_playing():
73+
print("stop_playing()")
74+
AudioPlayer._keep_running = False
75+
7076
# ------------------------------------------------------------------
7177
# Playback entry point (called from a thread)
7278
# ------------------------------------------------------------------
7379
@classmethod
7480
def play_wav(cls, filename):
81+
AudioPlayer._keep_running = True
7582
"""Play a large mono 16-bit PCM WAV file with on-the-fly volume."""
7683
try:
7784
with open(filename, 'rb') as f:
@@ -124,6 +131,11 @@ def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
124131

125132
total = 0
126133
while total < data_size:
134+
if total % 51 == 0:
135+
print('.', end='')
136+
if not AudioPlayer._keep_running:
137+
print("_keep_running = False, stopping...")
138+
break
127139
to_read = min(chunk_size, data_size - total)
128140
raw = bytearray(f.read(to_read)) # mutable for in-place scaling
129141
if not raw:

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import machine
22
import os
33
import _thread
4+
import time
45

56
from mpos.apps import Activity, Intent
67
import mpos.sdcard
@@ -90,6 +91,8 @@ def onResume(self, screen):
9091
print("Not playing any file...")
9192
else:
9293
print("Starting thread to play file {self._filename}")
94+
AudioPlayer.stop_playing()
95+
time.sleep(1)
9396
_thread.stack_size(mpos.apps.good_stack_size())
9497
_thread.start_new_thread(AudioPlayer.play_wav, (self._filename,))
9598

0 commit comments

Comments
 (0)