code.py
 1  # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  """
 4  CircuitPython single MP3 playback example.
 5  Plays a single MP3 once.
 6  
 7  Remove this line and all of the following docstring content before submitting to the Learn repo.
 8  
 9  INCLUDE THE MP3 FILE 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  imports/setup and all other setup options so that the example includes ONLY the appropriate list
16  of imports and the hardware setup. For example, a generic SAMD51 example should be:
17  
18      import board
19      import audiomp3
20      import audioio
21  
22      audio = audioio.AudioOut(board.A0)
23  
24      decoder = audiomp3.MP3Decoder(open("slow.mp3", "rb"))
25      audio.play(decoder)
26      while audio.playing:
27          pass
28  
29      print("Done playing!")
30  
31  The example content, as above, should contain NO commented out code, NO setup comment labels, and
32  NO other commented out setup code.
33  """
34  import board
35  import audiomp3
36  
37  # For most SAMD51 boards
38  import audioio
39  
40  audio = audioio.AudioOut(board.A0)
41  
42  # For most RP2040 and nRF boards
43  # import audiopwmio
44  #
45  # audio = audiopwmio.PWMAudioOut(board.A0)
46  
47  # For MacroPad, Circuit Playground Bluefruit, and any RP2040 or nRF boards with a built-in speaker
48  # and requiring you to enable the SPEAKER_ENABLE pin
49  # import audiopwmio
50  # import digitalio
51  #
52  # shutdown = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
53  # shutdown.switch_to_output(True)
54  # audio = audiopwmio.PWMAudioOut(board.SPEAKER)
55  
56  # For any SAMD51 boards with a built in speaker and requiring you to enable the SPEAKER_ENABLE pin
57  # import audioio
58  # import digitalio
59  #
60  # shutdown = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
61  # shutdown.switch_to_output(True)
62  # audio = audioio.AudioOut(board.SPEAKER)
63  
64  # For CLUE or nRF boards with built-in speaker and no SPEAKER_ENABLE pin
65  # import audiopwmio
66  #
67  # audio = audiopwmio.PWMAudioOut(board.SPEAKER)
68  
69  # For any SAMD51 boards with a built in speaker and no SPEAKER_ENABLE pin
70  # import audioio
71  #
72  # audio = audioio.AudioOut(board.SPEAKER)
73  
74  with open("slow.mp3", "rb") as mp3_file:
75      decoder = audiomp3.MP3Decoder(mp3_file)
76  
77      audio.play(decoder)
78      while audio.playing:
79          pass
80  
81  print("Done playing!")