Simple stopping method works

This commit is contained in:
Thomas Farstrike
2025-10-28 17:07:23 +01:00
parent c153bbef2d
commit 219b80ffb1
2 changed files with 15 additions and 0 deletions
@@ -11,6 +11,7 @@ class AudioPlayer:
# class-level defaults (shared by every instance)
_i2s = None # the I2S object (created once per playback)
_volume = 50 # 0-100 (100 = full scale)
_keep_running = True
@staticmethod
def find_data_chunk(f):
@@ -67,11 +68,17 @@ class AudioPlayer:
"""Return current volume 0-100."""
return cls._volume
#@classmethod
def stop_playing():
print("stop_playing()")
AudioPlayer._keep_running = False
# ------------------------------------------------------------------
# Playback entry point (called from a thread)
# ------------------------------------------------------------------
@classmethod
def play_wav(cls, filename):
AudioPlayer._keep_running = True
"""Play a large mono 16-bit PCM WAV file with on-the-fly volume."""
try:
with open(filename, 'rb') as f:
@@ -124,6 +131,11 @@ class AudioPlayer:
total = 0
while total < data_size:
if total % 51 == 0:
print('.', end='')
if not AudioPlayer._keep_running:
print("_keep_running = False, stopping...")
break
to_read = min(chunk_size, data_size - total)
raw = bytearray(f.read(to_read)) # mutable for in-place scaling
if not raw:
@@ -1,6 +1,7 @@
import machine
import os
import _thread
import time
from mpos.apps import Activity, Intent
import mpos.sdcard
@@ -90,6 +91,8 @@ class FullscreenPlayer(Activity):
print("Not playing any file...")
else:
print("Starting thread to play file {self._filename}")
AudioPlayer.stop_playing()
time.sleep(1)
_thread.stack_size(mpos.apps.good_stack_size())
_thread.start_new_thread(AudioPlayer.play_wav, (self._filename,))