Installing Music Player

Having sorted out how to get sound out of my previously mute Raspberry Pi, and been rather surprised at the result, the question was what to do with it. The obvious answer of course being to use it as a media player for my rather old and digitally challenged hi-fi.

Just to make things a little harder I decided to see if this was going to be possible with my original Raspberry Pi which only has 256MB RAM. I also wanted to be able to listen to the tracks on my desktop or laptop and check on the status of the media player and what was currently playing using a web browser (I’ll describe how I did that in a later article).

Of course no sooner had I managed to figure out how to go about doing this than someone else, who is obviously much better at this sort of thing than I am, came up with the RaspberryFi which looks awesome, though I’ve not tried using it yet. My efforts are a lot more modest but you may find these instructions useful as they should work equally well on a PC (or something else) running Debain Linux as they do on the Raspberry Pi.

Install the required packages

The hard work of actually playing the tracks will be done by the music player daemon (on UNIX or Linux a daemon is a process that runs in the background like a Windows service).

First you need to be running as root.

$ su


$ sudo -i

Then you need to install and configure the music player daemon.

# apt-get install mpd
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libao-common libao4 libasyncns0 libaudiofile1 libfaad2 libid3tag0
  libjson0 libmad0 libmikmod2 libmms0 libmpcdec6 libpulse0
Suggested packages:
  libaudio2 libesd0 libesd-alsa0 pulseaudio-esound-compat oss-compat
  pulseaudio avahi-daemon icecast2 mpd-client
The following NEW packages will be installed:
  libao-common libao4 libasyncns0 libaudiofile1 libfaad2 libid3tag0
  libjson0 libmad0 libmikmod2 libmms0 libmpcdec6 libpulse0
  libsndfile1 mpd
0 upgraded, 14 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,351 kB of archives.
After this operation, 3,352 kB of disk space will be used.
Do you want to continue [Y/n]? Y

Configure your music folder

By default your music files will go in /var/lib/mpd/music but I created a separate folder in the home partition, copied my music files from a USB stick, changed the file ownership and then modified the music directory entry in the configuration file.

# mkdir /home/music
# mount /dev/sda1 /mnt
# cp -R /mnt/ /home/music
# chown -R mpd /home/music
# umount /mnt
# vi /etc/mpd.conf

#music_directory                "/var/lib/mpd/music" 
music_directory                 "/home/music"

Configure the network

Note – Music player daemon is inherently insecure so you need to control who can access the system using a firewall, (even if you enable passwords for media player daemon they are passed in clear text so it isn’t worth bothering to use them!). To get around this issue I’d recommend that you use ssh to create a secure tunnel from the client machine to the server. I’ve included the instructions on how to do this further down the page.

Configuring music player daemon to use the network just requires one additional entry into the configuration file. You only need to do this if you intend to control the music player remotely, or plan on using it to stream music to your network.

# vi /etc/mpd.conf

# For network
bind_to_address        "localhost"
bind_to_address          "" # Server's IP address or
                                         # localhost
# And for Unix Socket
# bind_to_address                "/var/run/mpd/socket"
# This setting is the TCP port that is desired for the daemon 
# to get assigned to.
#port                           "6600"
port                            "6600"

At this point if all you want to do is have the music player daemon play tracks locally then you are done, unless you want to allow music player clients to connect without using ssh, in which case you will need to open up incoming connections to port 6600.

# ufw allow from to any port 6600 proto tcp

Setup streaming audio

Fortunately I discovered that not only is the ability to stream audio built-in to music player daemon, but that it should be possible for this to work on an Raspberry Pi. All that is needed are a few more changes to the configuration file. I found that on the Raspberry Pi you had to be very careful how the audio channels were configured, as well as which encoder and bit rate were selected. After some trial and quite a lot of error I found that the settings below seem to work quite well, and I used lame as this means that if I’m running Windows I can listen to the audio stream using Windows Media Player

# vi /etc/mpd.conf

# An example of a httpd output (built-in HTTP streaming server):
audio_output {
	type		"httpd"
	name		"Local"
#	encoder		"vorbis"	# optional, vorbis or lame
	encoder		"lame"
	port		"8000"
#	quality		"5.0"
	bitrate		"128"
	format		"44100:16:1"

You will also need to allow incoming connections to port 8000.

# ufw allow from to any port 8000 proto tcp

That should be it, for the changes to take effect you just need to restart the music player daemon. I have found that is seems to be a good idea to wait 5 seconds after it has stopped before starting it again.

# /etc/init.d/mpd stop
# /etc/init.d/mpd start

To start playing your tracks you will need to connect using a music player client, and select which tracks should be played from the media library. Once it is playing to listen to the streamed audio on another machine open the location in your media player (substituting the address of your server).

Connecting securely

To connect securely from a remote music player client you need to create a secure ssh tunnel from the client before starting the music player client application

$ ssh -N -L6600: username@server
username@server's password: 

This will create a secure tunnel from localhost port 6600 on the client to port 6600 on the the server. To connect simply configure the music player client to connect to localhost port 6600 (usually these are the default settings) and the connection will be forwarded to the server transparently, and securely, using ssh.

Note - After entering the password this command will appear to 'hang' as nothing else will happen (until you terminate it using ctrl-C), but it is working!

Raspberry Pi is a trademark of the Raspberry Pi Foundation

This entry was posted in Linux and tagged , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.