Thursday, July 31, 2014

Playing Audio on RPi

If omxplayer's auto-detection of the correct audio output device fails, you can force output over hdmi with:

$ omxplayer -o hdmi example.mp3

or you can force output over the headphone jack with:

$ omxplayer -o local example.mp3

To troubleshoot the sound module::
$ lsmod | grep snd_bcm2835
$ sudo modprobe snd_bcm2835

To load the sound module automatic:
cd /etc
sudo vi modules

# /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.


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

To play a WAV file using aplay:
$ aplay police_s.wav

To play an MP3 file using mpg123:
$ sudo apt-get -y install mpg321
$ mpg321 bubbling_water_1.mp3
$ mpg321 -g 50 bubbling_water_1.mp3     # [ volume 50% ]

To play a MP3 file using Imxplayer:
$ sudo apt-get -y install omxplayer
$ omxplayer bubbling_water_1.mp3

The plus (+) and minus (-) keys can be used to adjust the volume of the playback.