11

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

John Topley
  • 113,588
  • 46
  • 195
  • 237
Tony
  • 593
  • 2
  • 6
  • 16

2 Answers2

12

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.

ThomasCle
  • 6,792
  • 7
  • 41
  • 81
Dennis Munsie
  • 1,211
  • 12
  • 24
2

HI,

I think this video helps u in solving ur problem... IN WWDC videos they have clearly explained how u can enable the back ground audio... http://developer.apple.com/videos/wwdc/2010/ to view or download these videos u need to have a apple account... and in that see Session 109-Adopting multitasking on iPhone OS, Part2... Hope this will help u..

~Raviraja

Raviraja
  • 406
  • 2
  • 5
  • 17