code.py
 1  # SPDX-FileCopyrightText: 2020 Jeff Epler for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import os
 6  import time
 7  
 8  import board
 9  import digitalio
10  
11  # Updating the display can interfere with MP3 playback if it is not
12  # done carefully
13  try:
14      board.DISPLAY.auto_refresh = False
15  except: # pylint: disable=bare-except
16      pass
17  
18  from audiomp3 import MP3Decoder # pylint: disable=wrong-import-position
19  
20  try:
21      from audioio import AudioOut
22  except ImportError:
23      try:
24          from audiopwmio import PWMAudioOut as AudioOut
25      except ImportError:
26          pass  # not always supported by every board!
27  
28  # The mp3 files on the sd card will be played in alphabetical order
29  mp3files = sorted("/sd/" + filename for filename in os.listdir("/sd")
30      if filename.lower().endswith("mp3"))
31  
32  voodoo = [1,2,3]
33  
34  # You have to specify some mp3 file when creating the decoder
35  mp3 = open(mp3files[0], "rb")
36  decoder = MP3Decoder(mp3)
37  audio = AudioOut(board.A0, right_channel=board.A1)
38  
39  speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
40  speaker_enable.switch_to_output(True)
41  
42  while True:
43      for filename in mp3files:
44          print("Playing", filename)
45  
46          # Updating the .file property of the existing decoder
47          # helps avoid running out of memory (MemoryError exception)
48          decoder.file = open(filename, "rb")
49          audio.play(decoder)
50  
51          # This allows you to do other things while the audio plays!
52          while audio.playing:
53              time.sleep(1)