Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone OS 4 Multitasking - Playing Audio In background

Tags:

iphone

I am trying to use iPhone OS 4.0's multitasking capability. I tried to play audio in the background with no luck. I added UIBackgroundModes property in info.plist and mentioned requires audio to play in background. Also I added the code for playing the audio. `

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"someday" ofType:@"mp3"]]; 
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];

`. The audio starts playing once i click on button in the app. But when I shut the app it stops. How can I make it play in the background?

Thanks, Tony

like image 377
Tony Avatar asked Jun 09 '10 15:06

Tony


1 Answers

It sounds like you didn't set up your Audio Session correctly. From http://developer.apple.com/iphone/library/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html :

For example, when using the default audio session, audio in your application stops when the Auto-Lock period times out and the screen locks. If you want to ensure that playback continues with the screen locked, include the following lines in your application’s initialization code:

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

The AVAudioSessionCategoryPlayback category ensures that playback continues when the screen locks. Activating the audio session puts the specified category into effect.

like image 112
Dennis Munsie Avatar answered Nov 26 '22 20:11

Dennis Munsie