当AVAudioPlayer iOS 9正在播放前景状态时,音频不会在后台播放。
我已在Info.plist
中设置所需的背景模式'应用播放音频或使用AirPlay传输音频/视频我还检查了功能
中的音频,后台提取使用过的代码:
@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];
请提前帮助,谢谢。
答案 0 :(得分:0)
我使用以下代码在后台进行操作,并且不需要修改info.plist文件。
func setupPlayBackground(){
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error {
print(error.localizedDescription)
}
}