Playing background audio in iOS app


One of the most frequent requests I get for my AudioStretch for iOS app is the ability to continue playing when the phone is locked.  For some users it’s because their phone auto-locks after a minute or two; if you’re playing along with a slowed down guitar solo, perhaps looping indefinitely, it’s really annoying if it suddenly stops.  Other users want to manually lock their phone and listen to slowed down music with the phone in their pocket.  I’ve finally figured out how to do it!

Apple’s Audio Session Cookbook provides some, but not all, of the steps.  In particular, it mentions that “If you want to ensure that your audio continues when the screen locks and when the Silent switch is in the ‘silent’ position (on iPhone, this switch is called the Ring/Silent switch), you need to assign an appropriate category to your audio session.”  Specifically, you need to so create an AudioSession and use the kAudioSessionCategory_MediaPlayback category.  All pretty well explained in the Apple docs.

OSStatus err =
err = AudioSessionInitialize(NULL, kCFRunLoopDefaultMode, NULL, NULL);
assert(err == noErr);

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
err = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
assert(err == noErr);

But even if you follow those steps, you’ll find that your app will go silent as soon as it goes into the background. The other essential step is for your app to register itself as a background audio app so it will continue running in the background.  To do this include the UIBackgroundModes key (with the value audio) in your app’s Info.plist file. An explanation can be found in Apple’s documentation on App States and Multitasking, in the section “Playing Background Audio”.

UIBackgroundModes_pList

 

Note that the calls to assert() in the above code should normally be replaced with proper error handlers. Also, in order to properly handle inevitable interruptions to the audio output due to phone calls and alarms, in the call to AudioSessionInitialize() you should specify an AudioSessionInterruptionListener function as the third argument. In my listener, I call AudioOutputUnitStop() and AudioOutputUnitStart(), with the audio output tone unit as the argument, for interruption states kAudioSessionBeginInterruption and kAudioSessionEndInterruption respectively.

Advertisements

About Gerry Beauregard

I'm a Singapore-based Canadian software engineer, inventor, musician, and occasional triathlete. My current work and projects mainly involve audio technology for the web and iOS. I'm the author of AudioStretch, an audio time-stretching/pitch-shifting app for musicians. Past jobs have included writing speech recognition software for Apple, creating automatic video editing software for muvee, and designing ASICs for Nortel. I hold a Bachelor of Applied Science (Electrical Engineering) from Queen's University and a Master of Arts in Electroacoustic Music from Dartmouth College.
This entry was posted in Audio, iOS, iPhone, Programming. Bookmark the permalink.

2 Responses to Playing background audio in iOS app

  1. vamapaull says:

    Check your first link to audiostretch.com cause it’s not correct. You posted a link to audiostreth.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s