The Circuit Playground Express and Bluefruit have a built-in speaker above the music note printed on the board. It is the grey box with a + on it, below button A, to the left of the slide switch. Though the image is of the Circuit Playground Express, the speaker is in the same location on the Bluefruit. The speaker is also able to play monotone music encoded in a special format called wav files!

Sound files for the Circuit Playground library should be 22,050 kHz, 16-bit, mono (or less) WAV files to play on these boards. If you have an MP3 or a file you downloaded and are unsure of the encoding, you can follow this audio conversion guide to get your files into the proper format.

For testing, we've prepared two WAV files in the proper format. You can download the following two .wav files and copy them to your Circuit Playground CIRCUITPY drive.

Add the following code to your code.py. Remember, if you need help with this, check here.

"""THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
Adafruit_CircuitPython_CircuitPlayground REPO found at:
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples

Copy the "dip.wav" file to your CIRCUITPY drive.

Once the file is copied, this example plays a wav file!"""
from adafruit_circuitplayground import cp

cp.play_file("dip.wav")

Dip!

Let's look at the code. First we import cp.

Then, we play a wav file called "dip.wav" with cp.play_file("dip.wav"). That's it!

Note that we did not include a loop in this code. This is because if the code is in a loop, it will continue playing indefinitely. This is not always desirable, so we've designed the code to play the file once.

cp.play_file() requires one thing from you: the name of the wav file you're trying to play back in quotation marks. This is how it knows what file to play. So anytime you want to use it, you'll want to add cp.play_file("Filename.wav") to your code, replacing Filename.wav with the name of your wav file. It is case sensitive, so match the file name exactly.

Let's add some inputs and another wav file. Add the following code to your code.py.

"""THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE
Adafruit_CircuitPython_CircuitPlayground REPO found at:
https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples

Copy the "dip.wav" and "rise.wav" files to your CIRCUITPY drive.

Once the files are copied, this example plays a different wav file for each button pressed!"""
from adafruit_circuitplayground import cp

while True:
    if cp.button_a:
        cp.play_file("dip.wav")
    if cp.button_b:
        cp.play_file("rise.wav")

Now press button A. Dip! Press button B. Rise!

Inside the loop, we check to see if each button is pressed. If button A is pressed, we play "dip.wav". If button B is pressed, we play "rise.wav".

Notice if you press button B and then immediately try to press button A, the rise.wav file completes before you're able to dip again. This is because you cannot begin playing another file until the first file is completed. So, if you have a really long wav file, you'll find you can't do anything else until the file is finished playing. Keep that in mind if you're going to include wav files with other code.

You can use any of the inputs we've discussed to trigger a file to play. Try replacing the button presses with touch inputs. Try adding different files to use!

If your code is running but your file doesn't sound quite right or doesn't play back, be sure to check the encoding of your sound file by following this Adafruit guide.

This guide was first published on Jun 05, 2018. It was last updated on Jun 05, 2018.
This page (Play File) was last updated on Oct 21, 2020.