code.py
 1  # SPDX-FileCopyrightText: 2020 Jeff Epler for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """CircuitPython Essentials Audio Out MP3 Example"""
 6  import board
 7  import digitalio
 8  
 9  from audiomp3 import MP3Decoder
10  
11  try:
12      from audioio import AudioOut
13  except ImportError:
14      try:
15          from audiopwmio import PWMAudioOut as AudioOut
16      except ImportError:
17          pass  # not always supported by every board!
18  
19  button = digitalio.DigitalInOut(board.A1)
20  button.switch_to_input(pull=digitalio.Pull.UP)
21  
22  # The listed mp3files will be played in order
23  mp3files = ["slow.mp3", "happy.mp3"]
24  
25  # You have to specify some mp3 file when creating the decoder
26  mp3 = open(mp3files[0], "rb")
27  decoder = MP3Decoder(mp3)
28  audio = AudioOut(board.A0)
29  
30  while True:
31      for filename in mp3files:
32          # Updating the .file property of the existing decoder
33          # helps avoid running out of memory (MemoryError exception)
34          decoder.file = open(filename, "rb")
35          audio.play(decoder)
36          print("playing", filename)
37  
38          # This allows you to do other things while the audio plays!
39          while audio.playing:
40              pass
41  
42          print("Waiting for button press to continue!")
43          while button.value:
44              pass