Skip to content

Commit 4b1e35c

Browse files
authored
Merge pull request adafruit#1178 from jepler/cp-essentials-mp3
Add MP3 code sample to Essentials guide
2 parents d72cf6e + 340067e commit 4b1e35c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import board
2+
import digitalio
3+
4+
from audiomp3 import MP3Decoder
5+
6+
try:
7+
from audioio import AudioOut
8+
except ImportError:
9+
try:
10+
from audiopwmio import PWMAudioOut as AudioOut
11+
except ImportError:
12+
pass # not always supported by every board!
13+
14+
button = digitalio.DigitalInOut(board.A1)
15+
button.switch_to_input(pull=digitalio.Pull.UP)
16+
17+
# The listed mp3files will be played in order
18+
mp3files = ["begins.mp3", "xfiles.mp3"]
19+
20+
# You have to specify some mp3 file when creating the decoder
21+
mp3 = open(mp3files[0], "rb")
22+
decoder = MP3Decoder(mp3)
23+
audio = AudioOut(board.A0)
24+
25+
while True:
26+
for filename in mp3files:
27+
# Updating the .file property of the existing decoder
28+
# helps avoid running out of memory (MemoryError exception)
29+
decoder.file = open(filename, "rb")
30+
audio.play(decoder)
31+
print("playing", filename)
32+
33+
# This allows you to do other things while the audio plays!
34+
while audio.playing:
35+
pass
36+
37+
print("Waiting for button press to continue!")
38+
while button.value:
39+
pass

0 commit comments

Comments
 (0)