Skip to content

Commit 3c54032

Browse files
authored
Merge pull request #95 from FoamyGuy/play_mp3_for_cpb
add play_mp3 function to bluefruit module
2 parents d466ca9 + d2ab3a3 commit 3c54032

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

adafruit_circuitplayground/bluefruit.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import digitalio
4343
import board
4444
import audiopwmio
45+
import audiomp3
4546
import audiobusio
4647
from adafruit_circuitplayground.circuit_playground_base import CircuitPlaygroundBase
4748

@@ -151,6 +152,41 @@ def loud_sound(self, sound_threshold=200):
151152

152153
return self.sound_level > sound_threshold
153154

155+
def play_mp3(self, file_name):
156+
""" Play a .mp3 file using the onboard speaker.
157+
158+
:param file_name: The name of your .mp3 file in quotation marks including .mp3
159+
160+
.. image :: ../docs/_static/speaker.jpg
161+
:alt: Onboard speaker
162+
163+
To use with the Circuit Playground Bluefruit:
164+
165+
.. code-block:: python
166+
167+
from adafruit_circuitplayground import cp
168+
169+
while True:
170+
if cp.button_a:
171+
cp.play_mp3("laugh.mp3")
172+
elif cp.button_b:
173+
cp.play_mp3("rimshot.mp3")
174+
"""
175+
if file_name.lower().endswith(".mp3"):
176+
# Play a specified file.
177+
self.stop_tone()
178+
self._speaker_enable.value = True
179+
with self._audio_out(
180+
board.SPEAKER
181+
) as audio: # pylint: disable=not-callable
182+
mp3file = audiomp3.MP3Decoder(open(file_name, "rb"))
183+
audio.play(mp3file)
184+
while audio.playing:
185+
pass
186+
self._speaker_enable.value = False
187+
else:
188+
raise ValueError("Filetype must be mp3")
189+
154190

155191
cpb = Bluefruit() # pylint: disable=invalid-name
156192
"""Object that is automatically created on import.

adafruit_circuitplayground/express.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def _unsupported(self):
8787
# Express, they will result in the NotImplementedError raised in the property above.
8888
sound_level = _unsupported
8989
loud_sound = _unsupported
90+
play_mp3 = _unsupported
9091

9192

9293
cpx = Express() # pylint: disable=invalid-name

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"audiocore",
4141
"audiopwmio",
4242
"audiobusio",
43+
"audiomp3",
4344
]
4445

4546
# Add any paths that contain templates here, relative to this directory.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
This example plays mp3 audio files from the built-in speaker when the A or B buttons are pressed.
3+
4+
NOTE: This example does NOT support Circuit Playground Express.
5+
"""
6+
from adafruit_circuitplayground import cp
7+
8+
while True:
9+
if cp.button_a:
10+
cp.play_mp3("dip.mp3")
11+
if cp.button_b:
12+
cp.play_mp3("rise.mp3")

examples/dip.mp3

5.51 KB
Binary file not shown.

examples/rise.mp3

5.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)