Advertisement

Using a Raspberry Pi as an AirPlay Receiver

by

This Cyber Monday Tuts+ courses will be reduced to just $3 (usually $15). Don't miss out.

The Raspberry Pi is a versatile little computer that provides the perfect sandbox to start creating some fun and interesting projects. One popular project is as an AirPlay receiver, allowing us to stream audio from an iOS device or computer using iTunes to our Raspberry Pi that's connected to a set of speakers.

In this tutorial, I'll show you how to set up a Raspberry Pi to be used as an AirPlay receiver so you can stream audio from any iOS device, iTunes or compatible AirPlay software such as AirFoil.


Requirements

We'll need a few bits and pieces before we get started, all of which I've listed below:

  • Raspberry Pi
  • SD Card that's 4GB or larger
  • Micro USB cable and power source (or Micro USB power adapter)
  • HDMI cable and compatible display
  • USB keyboard
  • Some speakers
  • Ethernet cable
I'll be using a Raspberry Pi Model B with these additional cables and connectors.
I'll be using a Raspberry Pi Model B with these additional cables and connectors.

I'm going to be using the Raspbian operating system. Raspbian is a Linux distribution that has been tweaked specifically for the Raspberry Pi. It's lightweight and easy to use, with all of the built-in hardware already configured with drivers and ready to go.

An AirPlay receiver does not necessarily need to be wireless and the Raspberry Pi I'll be configuring will make use of the Ethernet port and be connected via a cable to my home's network.


Flashing Your SD Card

Before we can set up our Raspberry Pi, we must download the Raspbian operating system and flash it to a suitable SD card. We've previously published a step-by-step guide on exactly this in our previous tutorial "How to Flash an SD Card for Raspberry Pi". Check it out and follow the instructions to flash your SD card and return here once completed.


Booting Your Raspberry Pi

Now that your SD card has been flashed, it's time to start up your Raspberry Pi.

  1. Connect your Raspberry Pi to your chosen display using a HDMI cable.
  2. Connect your USB keyboard.
  3. Insert your SD card.
  4. Power up the Raspberry Pi using the Micro USB port.

Tip: You can use a HDMI-DVI cable if your preferred display does not have an HDMI connection.

Your Raspberry Pi will begin the boot sequence and you'll be presented with the Raspberry Pi Configuration Tool. There are a few options we need to alter here before we can continue.

The Raspberry Pi Config Tool allows us to configure some further options, including making use of all the space on the SD card.
The Raspberry Pi Config Tool allows us to configure some further options, including making use of all the space on the SD card.

Expand Filesystem

By default, a flashed SD card will only occupy up to about 2GB of space, with most of it being used. Even flashing an 8GB card, only 2GB will show up. To correct this, we must expand the filesystem to fit the entire card.

Doing this is very simple and is actually the first option within the configuration tool. Simply press enter on the first option and it will automatically expand the filesystem to fill the entire card. For an 8GB card, this provides an additional 6GB of free space.

Change User Password

This is an optional process but is one that is still worth performing so that your Raspberry Pi can remain secure. The default user is "pi" and the password is "raspberry" so changing it, even for something as inconsequential as this, is still recommended. You can select the second option using the cursor keys and pressing enter, where you'll then be prompted to enter a new password and confirm it.

Finish

Those are all the settings that we need to configure and you can go ahead and select Finish. This will cause your Raspberry Pi to restart.

Once it's restarted, you'll be presented with a simple command line asking you to log in.

Enter the username "pi" with the password you recently set and you'll then be logged in and ready to start.

Where Are All The Icons?

All of the following instructions will actually be performed within a command-line interface and as such, don't require any form of desktop environment. A graphical interface requires a fair amount of system resources so if it can be avoided, it makes the resources it would have used available for other processes.

As our Raspberry Pi is going to be nothing more than a receiver for network information, it doesn't require a graphical interface. We do, however, need a display just so we can see what we're entering into it.


Updating Software

Just like with a normal computer, one of the first things to do is check for any software updates. To do this, enter the following into the command line:

$ sudo apt-get update && apt-get upgrade

We're actually running two commands here, one after the other. The first, apt-get update, checks to see if there are any new packages (software) that is available, in the same way we would run Software Update on a Mac or Windows Update on a Windows PC to see if there are any new updates.

The second command, apt-get upgrade, downloads and installs the software updates that are available. Again, if we were using a Mac running Software Update, this is when we actually click "Download & Install".

Updating the software of our Raspberry Pi can take a little time and you'll probably see lots of lines of information appear on the display.
Updating the software of our Raspberry Pi can take a little time and you'll probably see lots of lines of information appear on the display.

This process can take a little time depending on the updates available and the speed of your internet connection so be patient while the process is running.


Installing Dependents

Now that our Raspberry Pi is up to date, we need to install some additional software that isn't included as standard. The software we're installing is:

  • avahi-utils
  • build-essential
  • chkconfig
  • git
  • libao-dev
  • libavahi-client-dev
  • libcrypt-openssl-rsa-perl
  • libio-socket-inet6-perl
  • libssl-dev
  • libwww-perl
  • pkg-config

That seems like an awful lot of software! Most of these packages are actually related in some way and you'll notice the same term cropping up in a few of the filenames. Explaining the purpose of all these packages would go far beyond the requirements of this tutorial (and likely be of little interest to some readers) but, sufficed to say, you require all of these in order to continue.

Tip: If you'd like to know what you're installing, all of these are public packages and more information can be found on the Debian Package Wiki

Installing Packages

To install these, we can do so in one (giant) command. Deep breath!

$ sudo apt-get install avahi-utils build-essential chkconfig git libao-dev libavahi-client-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libssl-dev libwww-perl pkg-config

Enter your password, if prompted, and Raspbian will get to work and download all of these files for you. Just like the updates, this can take some time to do.


ShairPort

Phew! If you've made it this far then congratulations are in order. The next step is to download a utility called ShairPort, this will enable the core AirPlay functionality that is needed and will allow us to stream audio to our Raspberry Pi.

At the command prompt, let's make sure we're where we need to be. Simply type cd and we'll be placed back into our home directory. It's unlikely we've moved from there but, just in case you've been exploring your Raspberry Pi, it will make sure we know exactly where we are.

We're going to use our newly-installed git command to download ShairPort from GitHub.

$ git clone -b 1.0-dev git://github.com/abrasive/shairport.git
Many open-source projects store their code on GitHub which is accessible to everyone.
Many open-source projects store their code on GitHub which is accessible to everyone.

The command tells Raspbian to download the ShairPort utility from GitHub. Again, going into detail about how GitHub works would go way beyond the scope of this tutorial.

We're now going to "make" our software which will install it for us to use.

First of all, let's navigate to the shairport folder we just created by downloading it.

$ cd shairport

Next, we're going to prepare it for installation and then install it. We'll string this command together into one so that we're not needing to enter it separately.

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

After a few minutes, ShairPort is now installed and ready to go.

Starting ShairPort

To start ShairPort, enter the following command:

$ shairport -a 'Raspberry JAM'

This starts ShairPort and names our new AirPlay device as, rather fittingly Raspberry JAM. Our Raspberry Pi will then state that it's "listening for connections" which means it's ready to go.

As you can see, my iPad is now able to stream audio to our Raspberry Pi.
As you can see, my iPad is now able to stream audio to our Raspberry Pi.

On my iPad, I've opened the AirPlay menu and can now see our new speaker up and running. To test it out, simply start streaming some audio to it and plug in your speakers or headphones.


Tweaking Audio

You should, by now, have some audio playing through your Raspberry Pi but its probably a little on the quiet side and increasing speaker volume merely distorts it. The audio output of the Raspberry Pi isn't the best quality and, by default, the volume is set very low.

We can tweak this by using the command alsamixer. To run it, we first need to stop ShairPort. Pressing Ctrl-C will stop ShairPort running completely and allow us to enter the following commands:

$ alsamixer

Using the up and down cursor keys, adjust the level to be around 75-80, any more and you risk distorting the audio considerably.

The volume for the built-in audio of the Raspberry Pi can be tweaked further. Don't increase it too much, though, or else it will sound too distorted.
The volume for the built-in audio of the Raspberry Pi can be tweaked further. Don't increase it too much, though, or else it will sound too distorted.

Press Esc to save the changes and quit, then re-run the previous command to start ShairPort. To save you a little time, simply use the up and down cursor key again on your keyboard and you can scroll though your previous commands.

Once you've restarted it, use your iOS device (or whatever you're streaming from) to stream audio back to the Raspberry Pi and the audio should be much louder without having to turn the speakers up.


Wrapping Up

There are some drawbacks with the setup so far. There's no Wi-Fi connectivity so our Raspberry Pi, and therefore our speakers, are under a physical restriction. Additionally, the audio quality from the Raspberry Pi's built-in audio is mediocre at best. These are limitations that have relatively simple workarounds and they'll be ones that we look at as our tutorials continue.

No matter what, by the end of this tutorial you'll have been able to configure a Raspberry Pi from scratch as an AirPlay receiver, letting you stream audio from any compatible device or software, just as you could with an AirPort Express or even an Apple TV.

Advertisement