views:

1013

answers:

4

I have added the UIBackgroundModes property in Info.plist to have an array entry of "audio" and have added the call to setup the audio session: [session setCategory: AVAudioSessionCategoryPlayback error: &error];.

However, the only test device I have is the iPod Touch 2G which doesn't support multitasking. I've tried the simulator but the music stops playing when I switch to Safari. But when I switch back to my app, the song continues playing to a farther location than when I left the app.

It seems to have continued playing in the background but I didn't hear the audio of my app while using another app (Safari).

+1  A: 

Did u added Required back ground mode key in info.plist... If not try adding this... Goto info.plist file and click add in that add "Required back ground modes" the click a small triangle and u will get item0 in that add "App Plays audio"... This works fine...

~Raviraja

Raviraja
Yes I mentioned that in my question. Would you happen to have example projects which I can run on my simulator which contains the correct setup? Thanks.
paul_sns
+4  A: 

I too had the same issue. In simulator the audio pauses and when you launch it back it resumes playback. But testing on device it worked perfectly and audio continued to play in background. The sample code which i have used is

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioPlayer play];
Tony
I saw the same behavior. Code that works on iPhone 4 will silently advance in the simulator.
drawnonward
I want to mention that this also works with the AVPlayer class which supports streaming. Setting the category will enable an audio streaming app to continue to play when the device locks or the app is suspended. Very helpful.
Matt Long
A: 

This worked even on the device for me.

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [audioPlayer play];

This and setting the info.plist key to enable background audio is that is needed to enable background audio playing in an iPhone app. However, if you wish to play a sequence of audio files then one would need to perform each playback operation as a background task like this:

UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];

//perform the necessary operations

[[UIApplication sharedApplication] endBackgroundTask:bgTask];

Nathan
+3  A: 

Background audio is not supported in iPhone simulator. Source: WWDC 2010, Session 109 - Adopting Multitasking part 2.

iamj4de