我正在我的应用程序中录制音频,包括前景和后台.我还处理AVAudioSessionInterruptionNotification以在中断开始时停止录制并在结束时再次开始.虽然在前台它按预期工作,当应用程序在后台录制并且我接到一个呼叫时,它不会在呼叫结束后再次开始录制.我的代码如下:
- (void)p_handleAudioSessionInterruptionNotification:(NSNotification *)notification
{
NSUInteger interruptionType = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
if (self.isRecording && !self.interruptedWhileRecording) {
[self.recorder stop];
self.interruptedWhileRecording = YES;
return;
}
}
if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
if (self.interruptedWhileRecording) {
NSError *error = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
NSDictionary *settings = @{
AVEncoderAudioQualityKey: @(AVAudioQualityMax),
AVSampleRateKey: @8000,
AVFormatIDKey: @(kAudioFormatLinearPCM),
AVNumberOfChannelsKey: @1,
AVLinearPCMBitDepthKey: @16,
AVLinearPCMIsBigEndianKey: @NO,
AVLinearPCMIsFloatKey: @NO
};
_recorder = [[AVAudioRecorder alloc] initWithURL:fileURL settings:settings error:nil];
[self.recorder record];
self.interruptedWhileRecording = NO;
return;
}
}
}
请注意,fileURL指向NSDocumentDirectory子目录中的新caf文件.配置背景模式音频.我也试过voip和沉默,都没有成功.
AVAudioSessionInterruptionTypeEnded块中的NSError是OSStatus错误560557684,我还没有找到解决方法.
任何帮助将非常感激.