Raspberry Pi as a music server

Since my beautiful Raspberry Pi first arrived (and if you don't know what one is, then go click!) I had sunk a lot of time into trying to get it to work as a music server with the off-the-shelf media distributions like Raspbmc. Every possible route seemed to end in niggles and complications, though; they were compounded by the fact that I don't have a HDMI monitor or old TV to plug the Pi into.

More recently, I learned about the music player daemon (MPD) on Linux machines, and wondered if this would provide me with a decent solution: get the lowest layer sorted, and then worry about the graphics later. The most comprehensive guide I could find still felt over-complicated; especially for anyone who might not want to, say, set up an NFS client at the same time (!) or "change[d] the main user name from pi to dockes" (!!) And even then, the guide spent more time on setting up a fixed IP address than actually getting a graphical interface working, which surely is the "wow" moment for most people.

The steps below will hopefully lead you to that "wow" moment, based on my own experience and some of the important and useful points distilled from that guide. I'll assume that you've got your Pi connected to the network, a power supply and the audio plugged into something: I won't cover that here.

Basic Pi software setup (1/3)

You'll need to download the "Raspbian" distribution of Debian from the Pi website, and get it onto an SD card to act as the Pi's hard drive. I won't cover that here either, but here's a setup guide for Raspbian.

Once you've booted the Pi, you'll then need to SSH in. Raspbian tries to call itself "raspberrypi.lan" on the network, so at a command prompt on your laptop or desktop, run:

ssh pi@raspberry.lan

If that doesn't work, then your router might be causing you problems (I've only tested this on Speedtouch and Technicolor routers.) The exact solution will depend very strongly on your router; as there are so many different makes and models, then - as you might have guessed - I can't cover that here!

Otherwise, enter the password when prompted. If you've never SSHed in before, run the configuration manager:

sudo raspi-config

In this "graphical" configuration manager, set the following:

  • expand the disk space to fill the whole of your SD card
  • get the manager to install the most recent version of itself

Finally, run the following to update the package repositories, upgrade all your packages, and reboot the Pi. This will ultimately kick you out of your SSH connection:

sudo apt-get update && sudo apt-get upgrade
sudo /sbin/reboot

Your Pi will rapidly reboot and you should be able to SSH back in, less than a minute later.

Installing MPD and playing music (2/3)

Installation

SSH back into the Pi, and run the following to install mpd and two command-line tools for talking to it. You can install ncmpc or ncmpcpp (the "pp" I think standing for "plus-plus" or "one bettter than ncmpc):

sudo apt-get install mpd mpc ncmpc

mpc is a simple type-a-command tool, where you type textual commands one at a time; ncmpc(pp) has on the other hand a textual "GUI", which works over SSH, and looks a bit like old "graphical" packages from the early 1980s.

When mpd starts, you might get an error a bit like the following:

[....] Starting Music Player Daemon: mpdlisten: bind to '[::1]:6600' failed: 
Failed to create socket: Address family not supported by protocol (continuing 
  anyway, because binding to '127.0.0.1:6600' succeeded)
Failed to load database: Failed to open database file "/var/lib/mpd/tag_cache": 
  No such file or directory

As far as I can tell this is something to do with the Pi's IPv6 networking configuration. The changes below will make it go away!

Configuration

Edit mpd's configuration file: I'm comfortable using vi; but if you can't cope with it, you can use sudo apt-get install ... to install a better editor, like nano or emacs.

sudo vi /etc/mpd.conf

In the configuration file, you want to find and then change the following entries:

music_directory         "/change/this/to/where/your/music/will/be"
...
playlist_directory	"/change/this/to/where/your/playlists/will/be"
...
bind_to_address         "any"

The first two directories can be anywhere on the Pi's hard drive (the SD card, remember!) With that new configuration in place, you should restart mpd:

 
sudo service mpd restart

When you do so, you should also hopefully find the "failed" error message I noted above is no longer visible.

Music

The only other requirement for those directories is that the playlist directory needs to be writeable by mpd. This is because when you ask mpc or another graphical client to add something to a playlist, it asks mpd, which adds it on your behalf: so it's really mpd that's adding the entry. It's not the most secure option, but you can run:

chmod a+rwx /playlist/directory/location

to make the directory writeable by other users. If you want a more secure option, just ask and I'll put something in the comments below.

If you now want to transfer a lot of music en masse into these directories, you can always unplug the Pi, remove the SD card and insert it into your laptop. You should then be able to find the relevant subfolder on the card, and copy your music across into it as if it were an SD card from a camera or portable music player! Alternatively

Oh, one more point: as far as I can tell, mpd doesn't check for new tracks unless you tell it to. So if you add any new music by any means, you need to tell mpd to update its records via a tool like mpc e.g:

mpc update

Now, if you run mpc or ncmpc, they'll spot your new tracks.

Working?

Anyway, if you've got to this point, you should have a running music server, playing music out of your audio socket. But there's one more step before we get to the "wow" moment!

Controlling your music server from a mobile, tablet or other laptop (3/3)

The best bit is that, if you can SSH into the Raspberry Pi, then you (and any other devices connected to the same router) can also use a nice graphical client to access it! There are loads of clients which "talk MPD" so I've just picked three:

  • Android: Droid MPD (free)
  • iOS: MPoD (free: I think a paid-for version has e.g. better display on iPads.)
  • Linux: gMPC (free; requires GNOME; works fine on Unity)

Specify "raspberrypi.lan" in these apps and they should be able to access it, if everything else above went OK. Moreover, if you don't like any of these apps, and you know how to set up a webserver, your next move could be to download e.g. a PHP application and set that up. But setting up web applications on a Pi is probably a whole other blogpost!

Anyway, this worked really well for me. I'm listening to it as I type. Hope it works just as well for you!

Comments

Hi, thanks for this very good howto, just a quick question....how can I keep my music playing after I have closed the ssh session.....I'm having issues....ncmpc doesn't seem to recognize or play playlists with *.pls even when its in the playlist directory......

Anyway I used cmus running inside tmux to play my web radio playlist....you can log off ssh and music will still be playing on your pi.......real cool
I must however add that this doesn't work with gui based audio players....just ncurses based

It was a while ago now and I don't have everything set up, but I think ncmpc is just a client for configuring the underlying mpd daemon. So the music should carry on playing even when you're not connected. If it really isn't doing that for you, then a screen session or similar might help?

Anyway, after setting up with ncmpc, I used to use a remote MPD client instead: the iPad one, but I had to return the hardware to my employer when I went freelance!