AVAudioRecorder does not record in the background after an audio session is interrupted

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.

+7
ios objective-c xcode avaudiorecorder avaudiosession
source share
2 answers

I added the following

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

before setting up AVAudioSession, and it worked. Still don't know what errors might appear.

+9
source share

Error 560557684 for AVAudioSessionErrorCodeCannotInterruptOthers . This happens when your background application tries to activate an audio session that does not mix with other audio sessions. Background applications cannot start audio sessions that do not mix with the audio recording of the foreground application, as this interrupts the sound of the application that is currently being used by the user.

To fix this, make sure your session category is set to one that is mixable, such as AVAudioSessionCategoryPlayback . Also remember to set the category option AVAudioSessionCategoryOptionMixWithOthers (required) and AVAudioSessionCategoryOptionDuckOthers (optional). For example:

 // background audio *must* mix with other sessions (or setActive will fail) NSError *sessionError = nil; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDuckOthers error:&sessionError]; if (sessionError) { NSLog(@"ERROR: setCategory %@", [sessionError localizedDescription]); } 

Error code 560557684 is actually 4 ascii 'characters! int 'in 32 bit integer. Error codes are listed in the file AVAudioSession.h (see also AVAudioSession ):

  @enum AVAudioSession error codes @abstract These are the error codes returned from the AVAudioSession API. ... @constant AVAudioSessionErrorCodeCannotInterruptOthers The app audio session is non-mixable and trying to go active while in the background. This is allowed only when the app is the NowPlaying app. typedef NS_ENUM(NSInteger, AVAudioSessionErrorCode) { ... AVAudioSessionErrorCodeCannotInterruptOthers = '!int', /* 0x21696E74, 560557684 */ ... 
+12
source share

All Articles