Making the Pi play some audio

If the Raspberry Pi plays an audio file, and I can’t hear it, is it working?

My objective this week was to make the Raspberry Pi play some sound out of its 3.5mm audio/headphone jack. Last week I was able to get the Pi to boot up and run a program, but this week I wanted to start interacting with the outside world.

To start, before I headed down to the ModLab I put a sample MP3 and two sample WAV files on a USB.  Once the Pi was booted up, it was easy to transfer those files from the USB to the SD card that runs the Pi (I was using a mouse-keyboard combo that uses only one USB port).

Luckily, my version of the Raspian operating system is pre-configured to be able to give audio output.  To make play one of the wave files I used the Linux terminal window, using the command:

aplay wavsample1.wav

But, no sound.  The Pi would tell me it was playing, but no sound was coming out of either my headphones or the small speakers I had found in the ModLab.  After a few minutes of different attempts and some frustration, I learned that the PI automatically sets the audio out as through the HDMI, so I needed to set it to use the headphone jack instead.  A little of google revealed that I could use:

sudo amixer cset numid=3 n

where n is 0=auto, 1=headphones, 2=hdmi.
So I changed n to 1 to have the sound out through the 3.5mm headphone jack.  And booya, the wav played.  Quietly, but it played.

The next step was to play the MP3.  The pre-loaded MP3 player with Raspian is omxplayer, but all attempts to get it to play the file resulted in the Pi telling me it was playing the file, but no sound.

So instead, I installed a different MP3 capable audio player, mpg321.  I first installed mpg321:

sudo apt-get -y install mpg321

And then could play the MP3 using:

mpg321 mp3sample.mp3

Unfortunately, the file plays very quiet (it is just a headphone jack..), so you need to hold the speakers up to your ears to really hear the song playing!


I tried to program this in Python, so I could then have a series of audio files play in a specified order/based on inputs/etc., but was unable to determine the syntax in Python before ModLab began wrapping up, so that will have to be for another day.


Next steps:

1) Have the WAV and MP3 files run via Python, so I can call the program from the Linux terminal and it can play a series of files.

2) Have the program run automatically when the Pi boots up, so that I don’t need to have the keyboard/mouse and monitor plugged in for the Pi to play the audio.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s