Using a Raspberry Pi as an AirPlay Speaker


A few weekends ago I was fed up with my office audio setup (still am, to a point) and decided I needed a better way of listening to music while I worked. And since I have entirely too many original Rasbperry Pi devices hanging around, I decided to put one to good use.

After all, with Apple killing off the AirPort range, a tiny, cheap audio extender might come in handy for many other purposes, and with a bit of tinkering you can also use the Pi itself as a low-power (and low throughput) access point.

Bolting On AirPlay Support

The first step I took (right after reflashing a minimal Raspbian 8 image and updating it) was to get AirPlay support up and running. As it turns out, the most straightforward way to do that these days seems to be with shairport-sync, which besides being able to sync audio across multiple devices (something I don’t need – yet) also seems to have the most reliable AirPlay implementation right now.

Installing it is simple enough:

# NOTE:run these as root (or prefixed with sudo)

# install required packages
apt-get install alsa-utils autoconf libtool libdaemon-dev libasound2-dev libpopt-dev libconfig-dev avahi-daemon libavahi-client-dev libssl-dev

# grab the source
git clone https://github.com/mikebrady/shairport-sync.git

# build it
cd shairport-sync
autoreconf -i -f
./configure --with-alsa --with-avahi --with-ssl=openssl --with-metadata --with-systemd
make

# create a user account for it and add it to the audio group
groupadd -r shairport-sync
useradd -r -M -g shairport-sync -s /usr/bin/nologin -G audio shairport-sync

# install and enable service
make install
systemctl enable shairport-sync

And bingo, you should see a new speaker named after your Pi (to change the name, just edit /etc/default/shairport, the file’s self-explanatory).

If, like me, you don’t particularly like the flat audio you get off the built-in 3.5” jack (or if you have a Raspberry Pi Zero), you can get a USB audio adapter.

I got one of these (UK, DE) because the cable tail makes it a better option for me, but your mileage may vary.

To get it working, get the device number for your adapter using aplay -l (it’s quite likely to be 1 on a standard Pi) and set that as the default by editing asound.conf:

# cat /etc/asound.conf
pcm.!default {
    type hw
    card 1
}
ctl.!default {
    type hw
    card 1
}

Supporting Android Devices

Since I do happen to use an Android phone now and then, I’ve been looking into adding Bluetooth support to the mix.

However, Raspbian has changed enough for me to have failed to get pairing to work consistently on the Raspberry Pi 3, so I’ll re-visit that when I have more time – but if you’ve got it working on Raspbian 8, drop me a line and I’ll update this post.


See Also: