Skip to content

Commit 1f46569

Browse files
committed
play_mp3_file() function
1 parent a0327ac commit 1f46569

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

adafruit_fruitjam/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def __init__( # noqa: PLR0912,PLR0913,Too many branches,Too many arguments in f
193193

194194
self.sd_check = self.peripherals.sd_check
195195
self.play_file = self.peripherals.play_file
196+
self.play_mp3_file = self.peripherals.play_mp3_file
196197
self.stop_play = self.peripherals.stop_play
197198

198199
self.image_converter_url = self.network.image_converter_url

adafruit_fruitjam/peripherals.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def __init__(self):
189189
except OSError:
190190
# sdcard init or mounting failed
191191
self._sd_mounted = False
192+
self._mp3_decoder = None
192193

193194
@property
194195
def button1(self) -> bool:
@@ -247,6 +248,19 @@ def play_file(self, file_name, wait_to_finish=True):
247248
pass
248249
self.wavfile.close()
249250

251+
def play_mp3_file(self, filename):
252+
with open(filename, "rb") as f:
253+
if self._mp3_decoder is None:
254+
from audiomp3 import MP3Decoder # noqa: PLC0415, import outside top-level
255+
256+
self._mp3_decoder = MP3Decoder(f)
257+
else:
258+
self._mp3_decoder.file = f
259+
260+
self.audio.play(self._mp3_decoder)
261+
while self.audio.playing:
262+
pass
263+
250264
def stop_play(self):
251265
"""Stops playing a wav file."""
252266
self.audio.stop()

0 commit comments

Comments
 (0)