How to play a audio file in the background?
How can I play an audio file in the background?
This is my code:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"caf"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
alarmAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
if(audioError != nil) {
NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
[alarmAudioPlayer setNumberOfLoops: -1];
[alarmAudioPlayer play];开发者_JS百科
}
}
But it doesn't continue playing when I multitask, but then resumes when I multitask back to the app.
Note: I know this is a duplicate of a old question but that answer didn't work for me.
Make sure you have set your application to utilize the background audio API. Add this entry in your app's Info.plist
:
精彩评论