play_mp3_file() function

This commit is contained in:
foamyguy 2025-08-25 18:50:43 -05:00
parent a0327ac8e6
commit 1f46569c1d
2 changed files with 15 additions and 0 deletions

View file

@ -193,6 +193,7 @@ class FruitJam(PortalBase):
self.sd_check = self.peripherals.sd_check self.sd_check = self.peripherals.sd_check
self.play_file = self.peripherals.play_file self.play_file = self.peripherals.play_file
self.play_mp3_file = self.peripherals.play_mp3_file
self.stop_play = self.peripherals.stop_play self.stop_play = self.peripherals.stop_play
self.image_converter_url = self.network.image_converter_url self.image_converter_url = self.network.image_converter_url

View file

@ -189,6 +189,7 @@ class Peripherals:
except OSError: except OSError:
# sdcard init or mounting failed # sdcard init or mounting failed
self._sd_mounted = False self._sd_mounted = False
self._mp3_decoder = None
@property @property
def button1(self) -> bool: def button1(self) -> bool:
@ -247,6 +248,19 @@ class Peripherals:
pass pass
self.wavfile.close() self.wavfile.close()
def play_mp3_file(self, filename):
with open(filename, "rb") as f:
if self._mp3_decoder is None:
from audiomp3 import MP3Decoder # noqa: PLC0415, import outside top-level
self._mp3_decoder = MP3Decoder(f)
else:
self._mp3_decoder.file = f
self.audio.play(self._mp3_decoder)
while self.audio.playing:
pass
def stop_play(self): def stop_play(self):
"""Stops playing a wav file.""" """Stops playing a wav file."""
self.audio.stop() self.audio.stop()