I decided to attach some speakers to my Raspberry Pi. I didn’t want to spend much so I picked up a pair of stereo speakers from my local Poundworld. They cost £1 exactly. They aren’t exactly the sort of speakers you would hook up to your HiFi system but for experimenting you can’t beat the value. They have a 3.5mm jack which plugs into the Pi’s analog audio connector.
In time I want to play sound files from my Python programs to provide audio notifications.
Install the Sound Drivers
Update : The latest Raspbian image enables the sound driver by default so this installation section can be ignored. Skip to “Playing a WAV file”.
In the Debian “Squeeze” image the ALSA sound drivers are considered experimental and have been disabled by default. To enable sound you need to type the commands below into the command line :
sudo apt-get install alsa-utils
That will install the required sound driver files and tools. Type “Y” when prompted. The following command will load the sound driver :
sudo modprobe snd_bcm2835
By default the output is set to automatically select the interface (HDMI if available otherwise analog). You can force it to use a specific interface using :
sudo 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 :
sudo amixer cset numid=3 1
You will need to run the modprobe command again when your Pi is rebooted/restarted.
Playing A WAV File
In order to play a WAV file we need a file to play. Type the following command to download a test WAV file :
sudo wget http://www.freespecialeffects.co.uk/soundfx/sirens/police_s.wav
Now we can play it :
sudo aplay police_s.wav
If you are lucky the sound will play through your speakers or headphones.
I tried a number of WAV files but only some of them played. Hopefully this will be resolved in future releases of the sound drivers.
5 Comments
Hi,
I have tried a number of ways to get sound from my Pi but to no avail. I’ve tried the above and the SchsimPlayer for MagPi …… I am using the analogue port as my HDMI is too far away lol….
The instruction work fine for me. I’ve tried to play mp3 with mplayer but it stopped after a few seconds. Playback with mpg123 went much better 🙂
Thanks to Matt, with these instructions, I was able to play a wav file on my raspberry debian squeeze.
Could you help me about the followong problem: when I play the same sound file through Mono c# with SoundPlayer object:
SoundPlayer player = new SoundPlayer(mySoundPath);
player.Play();
I get some sizzles for 0.5 s at the beginning, but the sound is played; then if I want to play another time the sound with the same, I get the following message:
alsa lib pcm unable to set hw params for playback: file descriptor in bad state
I found no solution but disposing the player then recreate a new one:
player.Dispose();
SoundPlayer player = new SoundPlayer(mySoundPath);
player.Play();
Thanks for help, best regards
Where did you get these speakers?
They were from a shop called “PoundWorld”. Everything costs one British pound.