Most games and gadgets use prerecorded sounds and songs to make the device more useful and fun. The Ready Set STEM API provides some simple functions to play recorded sounds that are on the SD card.
The main function you'll used to play sounds is:
Sound(sound_filename)
where sound_filename
is the name of the file on the SD
card that you want to play.
For example, to hear a cat meow, you can try this:
The sound file "Meow.wav" lives on the SD-Card in the default sound
directory, along with all the sounds on the SD card. If you'd like to find
out where this directory is located, you can write a little program to do
that, using the function sound_dir
:
And, from there you could go to the desktop and view that directory from
the File Manager. Or, you can do it using some code as well! Python provides
a function that will return a list of all the files in a directory, called
listdir()
:
If you run that program, you'll see a list of all the sounds you can play.
In addition, you can add your own sounds to the SD card. If your Raspberry Pi is on the network, you can download sound files from the web right onto the SD card. You can even copy them directly to the SD card. We won't go into to the details of downloading or copying here (you can find more info online) — but once you have a sound downloaded, you can play it simply by specifying its full location when creating the sound.
For example, if you had downloaded a sound file called "MyVeryOwnCoolSound.wav" and stored it in the /home/pi directory, you could do the following:
Sound
is very similar to a
Note
in that you can call the same functions to
play()
them, wait()
for them, or
stop()
them.