code.py
 1  # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  """
 4  CircuitPython multiple MP3 playback example.
 5  Plays two MP3 files consecutively, once time each.
 6  
 7  Remove this line and all of the following docstring content before submitting to the Learn repo.
 8  
 9  INCLUDE THE MP3 FILES IN THIS DIRECTORY IN A DIRECTORY WITH THE RESULTING CODE.PY FILE.
10  
11  Choose the setup section appropriate for the board into which this template code is going. The
12  default is for SAMD51 boards.
13  
14  If the setup is commented out, uncomment it. Regardless, ALWAYS delete the comment above the chosen
15  setup is commented out, uncomment it. Regardless, ALWAYS delete the comment above the chosen
16  imports/setup and all other setup options so that the example includes ONLY the appropriate list
17  of imports and the hardware setup. For example, a generic SAMD51 example should be:
18  
19      import board
20      import audiomp3
21      import audioio
22  
23      audio = audioio.AudioOut(board.A0)
24  
25      mp3files = ["slow.mp3", "happy.mp3"]
26  
27      with open(mp3files[0], "rb") as mp3:
28          decoder = audiomp3.MP3Decoder(mp3)
29  
30          for filename in mp3files:
31              with open(filename, "rb") as decoder.file:
32                  audio.play(decoder)
33                  print("Playing:", filename)
34                  while audio.playing:
35                      pass
36  
37  The example content, as above, should contain NO commented out code, NO setup comment labels, and
38  NO other commented out setup code.
39  """
40  import board
41  import audiomp3
42  
43  # For most SAMD51 boards
44  import audioio
45  
46  audio = audioio.AudioOut(board.A0)
47  
48  # For most RP2040 and nRF boards
49  # import audiopwmio
50  #
51  # audio = audiopwmio.PWMAudioOut(board.A0)
52  
53  # For MacroPad, Circuit Playground Bluefruit, and any RP2040 or nRF boards with a built-in speaker
54  # and requiring you to enable the SPEAKER_ENABLE pin
55  # import audiopwmio
56  # import digitalio
57  #
58  # shutdown = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
59  # shutdown.switch_to_output(True)
60  # audio = audiopwmio.PWMAudioOut(board.SPEAKER)
61  
62  # For any SAMD51 boards with a built in speaker and requiring you to enable the SPEAKER_ENABLE pin
63  # import audioio
64  # import digitalio
65  #
66  # shutdown = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
67  # shutdown.switch_to_output(True)
68  # audio = audioio.AudioOut(board.SPEAKER)
69  
70  # For CLUE or nRF boards with built-in speaker and no SPEAKER_ENABLE pin
71  # import audiopwmio
72  #
73  # audio = audiopwmio.PWMAudioOut(board.SPEAKER)
74  
75  # For any SAMD51 boards with a built in speaker and no SPEAKER_ENABLE pin
76  # import audioio
77  #
78  # audio = audioio.AudioOut(board.SPEAKER)
79  
80  mp3files = ["slow.mp3", "happy.mp3"]
81  
82  with open(mp3files[0], "rb") as mp3:
83      decoder = audiomp3.MP3Decoder(mp3)
84  
85      for filename in mp3files:
86          with open(filename, "rb") as decoder.file:
87              audio.play(decoder)
88              print("Playing:", filename)
89              while audio.playing:
90                  pass
91  
92  print("Done playing!")