I record audio in my application, both in the foreground and in the background. I also process AVAudioSessionInterruptionNotification to stop recording when the interrupt starts, and start again when it ends. Although in the foreground, it works as expected when the application is recording in the background, and I get a call that it does not start recording again after the call ends. My code is as follows:
- (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; } } }
Notice that the .URL file points to the new caf file in the NSDocumentDirectory subdirectory. Background sound is set. I also tried voip and played silence, and it did not work out.
The NSError block in AVAudioSessionInterruptionTypeEnded is an OSStatus 560557684 error that I did not find how to solve.
Any help would be greatly appreciated.
ios objective-c xcode avaudiorecorder avaudiosession
ozzotto
source share