Adding Airplay and Making It Play Nicely with MPD

Another feature I wanted to add to my Liva and Brix is airplay support. Luckily, there is a package that allows airplay called Shairport. Doesn’t linux have everything?

I like using mpd for most of my music playing. It handles high resolution music, and it works well with remote controls. I also like Airplay because it is convenient for just sending songs from other people’s iOS devices or even your own. Unfortunately, I’ve run into problems with them working well together on the same system. Both my setup of mpd and shairport play through ALSA. So if you’re already playing something on mpd and try to Airplay something, the shairport daemon just dies. That’s annoying. But there’s a way to fix this problem which I will cover.

Install necessary packages

sudo apt-get install libssl-dev libavahi-client-dev libasound2-dev git

Use git to get the master branch of shairport.

git clone -b master git://github.com/abrasive/shairport.git

Go to the shairport directory it just created.

cd shairport

Then make shairport.

sudo ./configure && sudo make && sudo make install

Daemonize shairport.

Shairport has a recommended way of daemonizing shairport. I found it just easier to use my own init script. Do this:

sudo nano /etc/init.d/shairport

Copy the following into the script.

#! /bin/sh
# /etc/init.d/shairport
#
### BEGIN INIT INFO
# Provides:     shairport
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Put a short description of the service here
# Description:       Put a long description of the service here
### END INIT INFO


NAME="Livang Room"

case "$1" in
  start)
    /usr/local/bin/shairport -b 95 -d -a "$NAME" -w -B "/usr/bin/mpc stop"
    ;;
  stop)
    kill -9 `pidof shairport`
    ;;
  *)
    echo "Usage: /etc/init.d/shairport {start|stop}"
    exit 1
    ;;
esac

exit 0
#

Some detail about the heart of the script and how to get mpd and shairport to get along.

In a nutshell, you’re just sending the following command to start up shairport.

/usr/local/bin/shairport -b 95 -d -a "$NAME" -w -B "/usr/bin/mpc stop"

Explanation of the options:

-b 95 — Adds a buffer which incidentally adds a little bit of latency before beginning to play. This is the amount of time I needed to make it sync with my other Apple Airplay devices.
-d — Runs shairport in the background or as a daemon.
-a "Airplay Name" — Allows you to customize the name of your airplay device.
-w — Waits for a command you specify to complete before playing your airplay.
-B "some command" — Allows you to specify the command that runs before airplay starts. In this example, I have it run mpc stop which will stop mpd if it is already playing. This should allow the two apps to get along. Inversely, you can use the option -E which will run a command after your shairplay session ends.

Finish it up

Make the script exectutable for all users.

sudo chmod a+x /etc/init.d/shairport

Make it run at start up.

sudo update-rc.d shairport defaults

Then reboot. Now your linux music player should show up as an Airplay receiver on all your iOS and Mac devices!

1 Comment

  1. Hi,

    Thanks for this modified as suggested to /usr/local/bin/shairport -b 95 -d -a “$NAME” -w -B “/usr/bin/mpc stop” -E “mpc play”
    and it now also starts mlc again after the shairport stream stops! exactly what I wanted – thanks again!

Leave a Comment

Your email address will not be published. Required fields are marked *