Skip to content

Commit e67239b

Browse files
authored
Merge pull request #15 from FoamyGuy/play_mp3_file
play_mp3_file() function
2 parents 8ee337c + b4f18f7 commit e67239b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-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
self.volume = self.peripherals.volume
198199
self.audio_output = self.peripherals.audio_output

adafruit_fruitjam/peripherals.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def __init__(self, audio_output="headphone", safe_volume_limit=12):
196196
except OSError:
197197
# sdcard init or mounting failed
198198
self._sd_mounted = False
199+
self._mp3_decoder = None
199200

200201
@property
201202
def button1(self) -> bool:
@@ -254,6 +255,18 @@ def play_file(self, file_name, wait_to_finish=True):
254255
pass
255256
self.wavfile.close()
256257

258+
def play_mp3_file(self, filename):
259+
if self._mp3_decoder is None:
260+
from audiomp3 import MP3Decoder # noqa: PLC0415, import outside top-level
261+
262+
self._mp3_decoder = MP3Decoder(filename)
263+
else:
264+
self._mp3_decoder.open(filename)
265+
266+
self.audio.play(self._mp3_decoder)
267+
while self.audio.playing:
268+
pass
269+
257270
def stop_play(self):
258271
"""Stops playing a wav file."""
259272
self.audio.stop()

0 commit comments

Comments
 (0)