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).
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];
Background audio is not supported in iPhone simulator. Source: WWDC 2010, Session 109 - Adopting Multitasking part 2.
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
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 what 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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With