Under The Microscope

Background Audio in iOS 4.0

Yesterday, Apple released version 4 of iPhone OS — now known as iOS — and with it comes multitasking. However, iOS doesn’t offer true multitasking in the sense we’re familiar with from desktop OSes, in which all apps execute simultaneously at all times.

In iOS, apps now suspend instead of terminating when you click Home. While in the background, they can perform a few types of specialized tasks, provided they follow certain rules — such as not monopolizing resources or using too much memory — and as long as the device supports it. Craig Hockenberry’s writeup goes into some detail on what exactly iOS 4 means by “multitasking,” and I recently wrote about what app suspension means for developers on my personal blog.

Fortunately for us, one of the types of multitasking iOS now supports is background audio. Audio apps can now continue to play audio after the user hits the Home button, and the user can now control that audio using the clicker on the headset cable, the lock screen controls (accessible by double-clicking the Home button while the device is locked), or the new audio controls in the leftmost section of the multitasking tray, pictured below:

iOS 4 Tray
To get to these buttons, double-click Home, then swipe the tray left-to-right.

We’re excited about background audio because it gives us an opportunity to improve Airfoil Speakers Touch, our free companion app to Airfoil. Using Airfoil Speakers Touch, you can receive audio on your iOS device from any computer running Airfoil.

Our first pass at testing background audio was almost ridiculously easy: the app just tells the system that it would like to play audio in the background, and then when the user clicks the Home button the app keeps running. This worked just fine, on the face of it.

In reality, however, it’s a bit more complicated than that. Because iOS 4 will terminate background apps when it runs low on memory, we must also take some steps to reduce our memory usage when we transition to the background. This isn’t just good citizenship; it’s enlightened self-interest, because the system will terminate apps that are using lots of memory first.

There are some other rules besides memory usage, of course, which are enforced by the OS. For example, only one app can play background audio at a time, though a second app can interrupt in order to play something brief like an alert sound or a turn-by-turn navigation announcement. More limiting, however, is this: if your app tells the system it would like to play audio in the background, then it must play audio. As soon as you stop playback, you get suspended.

When a background app gets suspended, it can no longer respond to network activity. In the case of a typical audio streaming app like Pandora Radio, this isn’t a big deal. When terminated by the OS, it will simply stop playing audio. When it resumes (upon being relaunched), it will just re-establish its connection and play audio again.

But for Airfoil Speakers Touch, which advertises its presence on the local network so that Airfoil can find and transmit audio to it, the consequences are worse. Airfoil will immediately stop transmitting to a suspended Airfoil Speakers Touch and remove it from its list of outputs. As a result, supporting a background Pause command in Airfoil Speakers Touch is problematic, because pausing severs our network connection with Airfoil.

One way we might solve this problem in the future has to do with an oft-requested feature for Airfoil: remote control of the source application. With Remote Control, you’d be able to send audio commands like Play and Pause to the audio source being streamed from Airfoil, via audio controls built into Airfoil Speakers. And on iOS, ideally we’d like to hook the background audio controls up to these Remote Control commands.

Schedule

Right now we are finishing up our work on an update for Airfoil Speakers Touch to allow it to play audio in the background. We hope to have that released to the App Store sometime in July. In the longer term, we’ll be working on an update to both Airfoil and Airfoil Speakers that will have, among other things, Remote Control support as outlined above.

So look for our free Airfoil Speakers Touch 1.1 update on the App Store soon, with basic background audio support. After that, we’ll hopefully have even better support in the not-too-distant future.

Our Software