Updated: May 19
I am writing an application that makes great use of AVFundation in iOS, my application needs to be able to play music in the background. I like to prototype quick and the iterate, so I got the basic skeleton working and a full audio queu class in c I can reuse.
I designed a little player class that extends AVAudioPlayer whitch I know I will have to update to use AVAssest, but I didn’t want to dive into that just yet. I wanted the thing to run and in a matter of hours, it did. I was able to load a bunch of audio files and play them tapping a table view, nothing fancy.
All was great, I refined and refined and I now have a very cool solid prototype I can show, now it was time to make it play in the background, when in lock screen or when some other non audio app is running. I started looking at Apple’s docs, usually the programming guide is a good birds eye view of the library. In AVFundation programmin guide I found that there are many audio technologies to use, the trick is to find out what is what.
After hours of digging I decided that I should use AVAudioPlayer, so I followed these steps.
1) info.plist has the UIBackgorundModes being an array with one item “audio”.
2) Explicitly set the session category to be AVAudioSessionCatergoryPlayback
3) Explicitly set the session mode to be AVAudioSessionModeDefault
After this I implemented “remote control” so you can control the app from the lock screen or command center and provided the necessary “now playing” info.
Awesome, it was time to test!
All was working without a hickup but the only problem was that the audio stopped then the application moved to the background or when locked.
So the search started, checking for object lifetime. Do we have the proper plist entries? What does apple say about it? Is the volume up? was it playing? Ok pull out the debugger, follow the acumulator, is it all there? Stack overflow, what am I missing? http://bit.ly/1X22oWT, have you tried this? Have you tried that? Yes but no luck. Suddenly I clicked the project icon on Xcode and saw “Capabilities”…
There was a pause, a brief pause, mini and I clicked the tab, scrolled down and: