Things have moved on a bit with the Raspbian image since I wrote my Raspberry Pi Speakers & Analog Sound Test post. So I decided to take another look as I wanted to start adding some audio to my experiments. I’m making slow progress with my remote controlled car project but I figured I might as well shoe-horn some audio in there as well.
One thing that has changed in the latest Raspian image is that the alsa sound drivers have improved and are enabled by default. So I decided to hook up a rechargeable powered speaker I had using a short 3.5mm jack plug lead and see if I could play some WAV and MP3 files.
The first thing to do is run :
lsmod | grep snd_bcm2835
and check snd_bcm2835 is listed. If it isn’t then run the following command :
sudo modprobe snd_bcm2835
If the module isn’t loaded automatically when you boot then you can force it to load by using the following process :
cd /etc sudo nano modules
Then add ‘snd-bcm2835’ so it looks like this :
# /etc/modules: kernel modules to load at boot time. # # This file contains the names of kernel modules that should be # loaded at boot time, one per line. Lines beginning with "#" are # ignored. Parameters can be specified after the module name. snd-bcm2835
By default the output is set to automatically select the default audio interface (HDMI if available otherwise analog). You can force it to use a specific interface using :
amixer cset numid=3 n
Where <n> is the required interface : 0=auto, 1=analog, 2=hdmi. To force the Raspberry Pi to use the analog output :
amixer cset numid=3 1
I usually have to do this if I boot the Pi with an HDMI cable plugged in. Otherwise it defaults to the 3.5mm jack automatically.
Playing A WAV File Using aplay
Everything should be ready to test some sound files. First up WAV. Use the following commands to grab some WAV files from my sound archive :
wget http://www.freespecialeffects.co.uk/soundfx/sirens/police_s.wav wget http://www.freespecialeffects.co.uk/soundfx/computers/bleep_01.wav
To play it use :
aplay police_s.wav
If you are lucky the sound will play through your speakers or headphones.
Playing An MP3 Using MPG321
There are numerous ways to play MP3s from the command line but I tend to use MPG321. This can be installed using :
sudo apt-get -y install mpg321
Once installed we can grab an MP3 to experiment with :
wget http://www.freespecialeffects.co.uk/soundfx/household/bubbling_water_1.mp3
The MP3 file can be played using :
mpg321 bubbling_water_1.mp3
The volume can be adjusted using the ‘g’ command line option. In the example below I set the volume to 50% :
mpg321 -g 50 bubbling_water_1.mp3
Playing An MP3 Using Omxplayer
You can also use the excellent Omxplayer to play MP3s. To get a list of options type :
omxplayer
It is installed by default in the latest Raspbian release but if you need to install it manually you can do so using :
sudo apt-get -y install omxplayer
To play a sound file you simply need to type :
omxplayer bubbling_water_1.mp3
The plus (+) and minus (-) keys can be used to adjust the volume of the playback.
This post covers basic audio playing from the command line. If you want to start playing lists of music then you should explore some of the more advanced media software available for the Pi. This includes XBMC and OpenElec.
In a future post I will cover playing audio within Python scripts using Pygame.
Here are some photos showing the rechargeable powered speaker I use for my Raspberry Pi command line audio tests :
18 Comments
Do you experience a click sound at the start and end of the sound played?
In the previous release of raspbian I do hear them
Regards
OWK
Hey, you can use cmus,it’s fantastic! http://cmus.sourceforge.net/
@OWK – I realize I am months behind your comment but I do not experience a pop with omxplayer.
Matt.
What directory would I copy an mp3 file to, so omx player can find it?
Thanks for info by the way.
You can use the same directory that you call omxplayer from.
I got omxplayer to find an mp3 file….. But, when I try to play it No sound. I put up volume using + on screen volume increases but still no sound.
I was able to play the file on the Scratch gui. So speaker and mp3 file seem ok. Just no sound with omxplayer.
Any ideas?
Sorted.. I typed in “omxplayer -o local” then the file name. Now works fine.
Next step to get monitor (hdmi) speaker operating.
Hi matt,
trying to make a music box for my 1.5 year old. Basically what I want is a keyboard connected to the PI and every time he presses the key, the song is playing. MP3s are preloaded in the player and each key corresponds to the particular file. I suspect that shell script should by embarrassingly simple but can’t make it work. Any Ideas? Thanks in advance.
The tricky bit will be stopping the current MP3 if a key is pressed before it is finished. Worth asking in the RaspberryPi.org forums as someone there will have some ideas.
You don’t need a full-sized keyboard; a number pad will do (especially the ones that do their own Num Lock and default to it being switched on; if yours doesn’t do this then you might need to set the Num Lock status with console tools). You can write the script in /root/.bash_profile and make root automatically log in to the console by putting this into /etc/inittab: 1:2345:respawn:/sbin/getty –noclear 38400 -a root tty1 (you’ll need to reboot or restart init before it takes effect). In the script itself, use “read -n1 -r Ans” to read one character from the keyboard and put it into the variable “Ans”, then do a switch (or if) with a load of tests on Ans (standard bash script stuff). Stopping a previous file from playing is merely a matter of writing “killall madplay” or whatever player you’re using.
Hey Matt,
What is the brand/model of the small speaker you have picture here?
– Peter
It is unbranded but I’ve just done a search on eBay for “rechargeable speaker 1.5W” and it comes up as the first result. The item title is “Sound Cube Portable Rechargeable MP3 Phone Mini Speaker”.
I want to use your sensor code
https://www.raspberrypi-spy.co.uk/2013/02/cheap-pir-sensors-and-the-raspberry-pi-part-2/
to play audio (mp3 or wav) when a motion is detected.
I have mpg321 installed and working when I run it directly from bash on the raspi.
But I can’t get it to work inside the python script. I have also tried using pygame, as some web pages suggest for audio playback through python. But that didn’t work either. I’ve drained two hours on trying to get this one thing working so it is time to ask for help.
I am using the tbo player gui as the front end for OMX player. It has play, pause, stop, vol+ and vol- ; however, when playing videos, the screen output most-often covers the gui.
When I started to type this post, I was on the wrong keyboard, and I was typing in my handle when the music stopped. I learned that the key stops the playback, (re)starts it, and pauses in midstream and then if repeated starts at the same point in the stream.
Ooops! I set the letters in less than and greater than tags, and this is html scripting.
r – starts the stream
i – stops playback
o – restarts at the beginning
p – pause – it’s a toggle, pause/un-pause
works fine with mp3 but a bit tricky with flv. To stop a flv file, first hit pause and then stop (p, then i) or else it just restarts.
There may be other keys.
Matt, much thanks for starting this discussion. (Now to find out about usb audio and also audio controls.
Thanks! Helpful post.
Very helpful, thanks for the simple steps and sample files!
Hello all!
Any ideas for playing music from Internet?
Some kind of cli spotify?