-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_out_mp3_file_code.py
46 lines (33 loc) · 1.08 KB
/
audio_out_mp3_file_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import board
import digitalio
from audiomp3 import MP3Decoder
# Handle import errors for different boards:
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
pass
button = digitalio.DigitalInOut(board.A1)
button.switch_to_input(pull=digitalio.Pull.UP)
# Create a list of mp3 files that will be played in order:
mp3_files = ["Sample001.mp3", "Sample2.mp3"]
# Specify an mp3 file when creating the decoder:
mp3 = open(mp3_files[0], 'rb')
decoder = MP3Decoder(mp3)
audio = AudioOut(board.A0)
# Event loop:
while True:
for filename in mp3_files:
# Updating the .file property of the existing decoder
# helps avoid running out of memory (MemoryError exception)
decoder.file = open(filename, "rb")
audio.play(decoder)
print("playing", filename)
# This allows you to do other things while the audio plays!
while audio.playing:
pass
print("Press button to continue!")
while button.value:
pass