Entertainment
Brought to you by

Turn A Raspberry Pi Into An AirPlay Receiver For Streaming Music

Few things are better than kicking back on the couch and streaming your favourite album wirelessly to your stereo from your phone. That’s easy to do with AirPlay, but if you don’t want to pay for Apple’s solutions, a Raspberry Pi does the job remarkably well.

It’s Raspberry Pi Week at Lifehacker, and we’ll be showing you some awesome DIY projects you can put together with this miraculous device. If you haven’t acquired one yet, check out our introduction to the Pi to learn more about what it is, what you’ll need, and the cool stuff you can do with one such as setting up a personal VPN and turning it into an XBMC media center.

Check out the video above for a quick demo of what this project entails, and what you’ll get with the final product. With a few tweaks to your initial Raspberry Pi installation, you can turn it into an amazing little device that can receive audio from any AirPlay-compatible device. That means wireless streaming to any speakers you have in the house. Music in the video by Comfort Fit and Jonathan Coulton.

What You’ll Need

Getting the AirPlay receiver up and running on the Pi is easy, but you’ll need a few materials first:

  • A Raspberry Pi: If you’re not sure where to buy one, check out our introduction to the Pi for more info.
  • An HDMI or composite video cable: You’ll need this cable to connect your Raspberry Pi to your television or monitor temporarily to get the Raspberry Pi set up.
  • An 8GB Class 10 SD card (or better) and a card reader (if you don’t have one built into your computer): You can go smaller or slower, but an 8GB Class 10 card will get you the best performance, and they’re pretty cheap. Most SD cards will work, but some aren’t compatible and will therefore cause issues. You can find out which cards are compatible, or locate a place to buy a compatible card with an operating system pre-installed, on this page.
  • A USB keyboard and mouse: Any standard USB keyboard or mouse will do. You’ll only need these for the setup process, so whatever you have attached to your computer right now should do the trick.
  • A good quality, micro USB power supply that can provide at least 700mA at 5V: Most modern smartphone chargers supply 700mA at 5V, but not all do. Check the bottom of your charger and look for a block of text. You’ll see its output values in that text which may read 0.7A instead of 700mA). If it offers at least that much power, you’re good to go. Don’t use a poor quality charger or you may run into problems.
  • A 3.5mm stereo audio cable : You need this to run the Raspberry Pi into your stereo.
  • Wi-Fi USB Adaptor: The Wi-Fi USB adaptor makes it possible to send your AirPlay music from your iOS device to your Raspberry Pi. I used this Edimax one and it worked well; you can find a full list of compatible adaptors here.
  • USB sound card: The audio output on the Raspberry Pi isn’t that great, so if you want to beef up your sound you should grab a USB sound card. You can find a list of compatible cards here. I used this cheap generic sound card, but a USB DAC will really clean up the sound nicely.
  • Stereo (and speakers): Obviously you’ll need to send your sound somewhere, so you’ll need a stereo and speakers that allow an audio input.
  • An iOS device or computer: You’ll need an iPhone, iPod Touch or iPad, or a computer with iTunes installed as a source for your music. Theoretically an Android device with software like doubleTwist would also work, but we haven’t tested it.

What You Will (And Won’t) Get

The Raspberry Pi makes a fantastic AirPlay device. When you’ve set it up, you’ll have a small box connected to your stereo that functions in a similar fashion to AirPlay-enabled speakers. When it’s plugged in and booted up it will automatically load everything necessary to start AirPlay with no need for an external monitor or keyboard. It lets you stream music to any speakers you own for less than half the price of an AirPort Express, and you can still use it for any other Raspberry Pi project you want to take on. That’s a serious win-win. The one downside? The Raspberry Pi doesn’t currently support AirPlay Mirroring to send video content.

Step One: Configure The Raspberry Pi

The AirPlay function works with the Raspberry Pi installation of Raspbian. You can follow our guide for getting started with the Raspberry Pi to install Raspbian, but there are some additional steps to follow.

To make the AirPlay receiver start automatically without the need for a keyboard or monitor, we need to set Raspbian to log you in automatically. During the setup process at the Raspi-config screen, change the “Start Desktop on Boot?” setting to yes. If you’re already up and running, type this command into the terminal to load up Raspi-config again:

sudo raspi-config

Finally, the AirPlay functionality may require a few packages not initially included in your installation, so you’ll want to update your packages before you get started. Type these commands into the console:

sudo apt-get update
sudo apt-get upgrade

The update might take a little while, so grab a beverage and sit back. Once you’re booted up into Raspbian and everything is working properly, we can start setting up the AirPlay function.

Note: It’s possible to install everything you need using console commands, but we’ll use the slightly easier Raspbian interface so that you can continue to use your Raspberry Pi for other roles as well.

Set Up Your USB Wi-Fi Adaptor

First off, we need to get the USB Wi-Fi adaptor working. This is very simple with Raspbian installed:

  1. Connect your USB Wi-Fi adaptor to the Pi if it isn’t already.
  2. Open the WiFi Config application on the desktop.
  3. Select your adaptor from the drop down list.
  4. Sign into your home network.

That’s it for the Wi-Fi adaptor. If you like, open up the Midori browser from the desktop to make sure the internet is working. Raspbian will remember your selection, so even if you disconnect the Wi-Fi adaptor at any time during the process it will automatically load up again when it is connected to the Pi.

Set Up Your Sound Card

Next up we need to get the sound card up and working. First, connect your USB sound card into the Raspberry Pi, and hook it up to your stereo with the 3.5mm audio cable (or plug in headphones). Next, we’re going to use a terminal command to select it. Open up LXTerminal from your desktop and type in the following command:

aplay -l

Your USB card should display as something like, “card 1: set [device name], device 0: USB Audio.” That means it has been recognised by the Raspberry Pi. Now we need to test the sound. Type:

Alsamixer

This loads up the software to test the audio output. Press F6 to change the output type and select your sound card. Now we need to test the output:

speaker-test

You should hear a tone from your speakers verifying that the USB sound card is working.

Finally, Raspbian doesn’t automatically load the USB sound card when it boots. This means we have to edit the configuration file. Type the following:

cd /etc/modprobe.d
sudo nano alsa-base.conf

This opens the alsa-base.conf file. Scroll down towards the end of the file and look for this line:

options snd-usb-audio index=-2

Comment that line out with a “#” symbol at the start of the line. Hit Ctrl+X to save and exit.

Now your sound card and Wi-Fi adaptors will both work automatically every time you start the Pi.

Install The Shairport AirPlay Emulator

Shairport is the software that actually makes AirPlay work, and it takes a little while to set up. It’s not a difficult process, but you will need to give yourself about 30 minutes because it requires a lot of downloading and installing of software.

We need to install a few items before we can get Shairport. Type this into the console:

sudo apt-get install git libao-dev libssl-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libwww-perl avahi-utils libmodule-build-perl

When it’s complete, we need to install an update so Shairport will work with iOS 6 (you can skip this step if you’re using iOS 6):

git clone https://github.com/njh/perl-net-sdp.git perl-net-sdp
cd perl-net-sdp
perl Build.PL
sudo ./Build
sudo ./Build test
sudo ./Build install
cd ..

Once the iOS 6 module is installed (give it a little while), it’s finally time to get Shairport installed. From your home directory type:

git clone https://github.com/hendrikw82/shairport.git
cd shairport
make

Now, let’s run Shairport with this command:

./shairport.pl -a AirPi

This command starts Shairport with your Raspberry Pi named “AirPi” (you can change it to whatever you want). Grab your iOS device, pick the music app of your choice, and tap the AirPlay button. You should see “AirPi” listed as an output device. Tap that, and the music should start streaming from your USB sound card within a couple of seconds.

We’re not quite done yet. Shairport doesn’t automatically load when you start your Raspberry Pi, and since we want to make our AirPlay device work without any peripherals we need to perform one more step. From your home directory, type:

cd shairport
make install
cp shairport.init.sample /etc/init.d/shairport
cd /etc/init.d
chmod a+x shairport
update-rc.d shairport defaults

Finally, we need to add Shairport as a launch item. Type:

sudo nano shairport

This loads up the Shairport file we need to edit. Look through the file for the “DAEMON_ARGS” line, and change it so it looks like this:

DAEMON_ARGS="-w $PIDFILE -a AirPi"

Save the file (Ctrl+X), and you’re all set. Shairport should launch on startup every time. You can now move the Raspberry Pi anywhere you like and do whatever else you want with it. As long as the USB sound card and Wi-Fi adaptor are plugged in, they will load up when you turn the Raspberry Pi along with Shairport. That means you don’t need a monitor, keyboard or mouse next to your stereo for it to work.

Once you’re set up, all you need to do to use the AirPlay function is plug the Raspberry Pi in, and wait for it to boot up. Once the operating system loads (it usually takes about 30-40 seconds), you can instantly and easily send all your music through the Raspberry Pi.

Take It Further

The basic AirPlay functionality is just the tip of the iceberg. You can do a lot more with it once the functionality is set up. Here are a few different ideas that expand the AirPlay functionality in the Raspberry Pi way past the AirPort Express:

A big thanks goes out to Jordan Burgess and Trouch for their guides to getting Shairport repositories installed and adding iOS 6 support.