Merge pull request #15 from FoamyGuy/play_mp3_file
Some checks failed
Build CI / test (push) Has been cancelled

play_mp3_file() function
This commit is contained in:
Scott Shawcroft 2025-08-28 15:43:20 -07:00 committed by GitHub
commit e67239b37b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 0 deletions

View file

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

View file

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