Reputation: 1012
Audio is not playing in background using AVAudioPlayer iOS 9 while it is playing in foreground state.
I have already set Required background modes 'App plays audio or streams audio/video using AirPlay' in Info.plist
I have also checked Audio, Background fetch in Capabilities
Used code:
@property (nonatomic, strong) AVAudioPlayer *ringtonePlayer;
NSString *ringtonePath = [[NSBundle mainBundle] pathForResource:@"incoming" ofType:@"wav"];
if ([ringtonePath length] <= 0) {
NSLog(@"Can't find incoming sound file");
return;
}
self.ringtonePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:ringtonePath] error:nil];
self.ringtonePlayer.delegate = self;
self.ringtonePlayer.numberOfLoops = -1;
NSError *error = nil;
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
error:&error]) {
NSLog(@"Unable to reroute audio: %@", [error localizedDescription]);
} else {
[[AVAudioSession sharedInstance] setActive:YES error:&error];
}
self.ringtonePlayer.volume = 1.0f;
[self.ringtonePlayer play];
Please help, thanks in advance.
Upvotes: 0
Views: 207
Reputation:
I use the following codes to paly in the background,and don't need to modify the info.plist file.
func setupPlayBackground(){
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error {
print(error.localizedDescription)
}
}
Upvotes: 1