Background Audio in iOS 4.0
Posted By Dan Wineman on June 22nd, 2010
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:
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.
Daniel says:
June 22nd, 2010 at 6:37 pmWhile I won’t be able to take advantage of the background audio support (iPhone 3G), this sounds fantastic. Keep up the good work!
You’ve spoken before about the difficulty of having Airfoil interface with different audio sources; are you just going to pass the play/pause commands in Speakers Touch to an Applescript, the way you do for the Apple TV & Airport Express? Or are you going to write code to control various popular audio sources (good luck with Spotify!)?
Austin says:
June 22nd, 2010 at 6:52 pmWell, you rock. Thanks for keeping support up, and for having the itch of excellence. Love the app, can’t wait for the update.
@Daniel: Yeah, the extra ‘reverse direction’ control would be a huge pain if they have to do more than tap into the OS-level play/pause/skip controls. Frankly, just being able to jump to the iTunes remote app without breaking my airfoil connection would be rad.
Dan Wineman says:
June 22nd, 2010 at 11:58 pm@Daniel: Basically, yes. We might also be able to control audio sources like Spotify by generating keyboard events, but for now, AppleScript is how we’re planning to do it.
@Austin: We appreciate the compliments!
Joel says:
June 23rd, 2010 at 6:33 pmHey, love your app! I’m using my 3GS with 4.0, and was wondering if it’s possible for you to make an app that does the opposite of airfoil – transmits audio FROM the ios device to another source. So I could be watching a movie on my iPhone, and have a docked iPod touch that receives audio a pumps it through my speakers. Or streams from iPhone to computer plugged into speakers.
Just a thought. Love the app, keep it up!
Dan Wineman says:
June 23rd, 2010 at 9:05 pm@Joel: We’d like to, but unfortunately the iOS SDK doesn’t allow that sort of thing. See this page for a more complete explanation.
Jim S. says:
June 26th, 2010 at 8:06 pmThanks for keeping me (your users) informed. Airfoil, Radioshift, Airfoil Speakers (Pulsar too but I don’t use it any more because I am dis-satisfied with XM pricing and service) and its related iPod/iPhone app are among my favorite software (ever!). When you do it, you do it right! Looking forward to the new version of Airfoil Speakers whenever you are ready to release it.
Tim Klein says:
June 27th, 2010 at 10:33 amVery very cool. Good luck with the update!
Adam Crabb says:
June 28th, 2010 at 4:48 pmThis is awesome! I was hoping you guys would be able to do this when OS 3 came out. When I heard that OS 4 was finally going to support background audio for developer apps, my first thought was: I hope Rogue Amoeba takes advantage of this with Airfoil Speakers Touch. Thanks for jumping on this right away–much appreciated!
Justin Luey says:
June 30th, 2010 at 4:44 amGreat news!
We really need Fission and RadioShift on the iPhone and iPad. Please make it happen Rogue Amoeba. Apple has really cleaned up their approval process. Please come back.
Paul Kafasis says:
June 30th, 2010 at 10:25 amJustin Luey: I’m afraid we have no plans for either of those apps on the iOS. Sorry. The approval process has improved slightly, but “really cleaned up” is an overstatement. More importantly, from a business prospective, it still doesn’t make sense for us.
David M. Cotter says:
June 30th, 2010 at 4:32 pmwell, when you stop receiving audio from the server, why not just keep playing silence?
Ross Collins says:
June 30th, 2010 at 4:36 pmThanks, but keep OS 3 support for us poor original iPhone users.
Matt Reimer says:
June 30th, 2010 at 4:45 pmCan’t you have your app claim to be a ‘voip’ app which allows you to keep network connections alive?
moole says:
June 30th, 2010 at 5:43 pm@Dan Wineman: You could easily hijack all iphone audio with help of a little hw mod – connecting pins of audio output with pins of audio input either in the dock connector or in the headset jack. Though i’m not sure, if there are any such loopback jacks available on the market now..
Ben says:
June 30th, 2010 at 8:19 pmWill there be any update to the remote playback app (or a separate app?) to allow for control of the desktop application? I use Airfoil with my AP Express as the primary audio source for my home theater system. The remote iOS app is great for controlling iTunes but being able to manage the connected application, zones, and volume for the entire airfoil system would be an amazing improvement!
Thanks, Ben
Marcus says:
June 30th, 2010 at 10:40 pmHello! I’m a paying Airfoil user who would really, really like Remote Control via Airfoil Speakers Touch. You said you’re planning on doing that in the “longer term”, but if you could just add volume control to your 1.1 update, I’d be a happy camper!
You could even restrict it to only global volume changing of all speakers connected to Airfoil as far as I’m concerned ala “linked to system volume mode”, if it’d make it easier.
Peter Witham says:
June 30th, 2010 at 11:09 pmSo glad to read this post, when background tasking was announced for iOS4 I immediately thought of Airfoil since I use it so much. Having remote control of the source has also been a long time wish for me as well :)
Keep up the great work, you remain a shinning example of OS-X application excellence.
Dan Wineman says:
July 1st, 2010 at 2:23 amDavid M. Cotter: We could, but the bigger issue is that “pause” doesn’t really make sense with an app like Airfoil. Without remote control, it’s more like “mute” since the source application keeps playing. So we’re holding off until we can do it right.
Ross Collins: Not to worry. For now at least, we’ll continue to support 3.0 and up.
Matt Reimer: Probably not if we expect Apple to approve it.
Ben: Stay tuned.
Marcus: Unless I misunderstand you, we actually already have volume control via Airfoil Speakers (and Airfoil Speakers Touch). The volume slider in Speakers is linked to the slider in Airfoil for that receiver, over the network.
Peter Witham: Thanks for the very kind words.
Tim Armes says:
July 1st, 2010 at 3:24 amHi,
Thanks for the article. Personally I’m looking for the inverse – I’d like to stream audio from my docked (and charged) iPhone to my aiport express. I can’t believe that the iPhone doesn’t already offer this.
Is this something that’s feasible? I doubt it…
Tim
Paul Kafasis says:
July 1st, 2010 at 10:30 amTim: Unfortunately, this really isn’t possible on iOS, due to Apple’s limitations. We have no way of getting audio from other applications. Even sending audio from the iTunes library is not really possible yet.
http://www.rogueamoeba.com/support/knowledgebase/?showArticle=AirfoilforiPhone
shuckster says:
July 1st, 2010 at 1:04 pmIf Airfoil *must* play audio to avoid suspension, couldn’t you just load a “silent” clip into the App for local playback when the server is paused?
Paul Kafasis says:
July 1st, 2010 at 2:10 pmshuckster: As Dan said above:
“We could, but the bigger issue is that “pause” doesn’t really make sense with an app like Airfoil. Without remote control, it’s more like “mute” since the source application keeps playing. So we’re holding off until we can do it right.”
Jim Thorpe says:
July 1st, 2010 at 7:29 pmI noticed today in the update to Last.fm that whenever you press the pause button, iPod.app immediately takes back the music controls. Seems like a similar situation to Airfoil Speakers Touch: as soon as the audio stops playing, the app is suspended and you have to switch to it to start things up again.
The same is not true of Pandora.app. Tell the device to pause Pandora, wait a minute or two, and you can still un-pause Pandora. It’ll continue to touch the network to get subsequent songs.
Does this mean Pandora is leveraging something to which you don’t have access?
Jim Thorpe says:
July 1st, 2010 at 7:32 pmActually, no, now I think about it, both those apps already do have a play/pause/skip control of their own, which is what you’re looking into now. Kinda looks like Last.fm just didn’t implement it fully with the iOS 4 functionality.