Unable to restart interrupted audio input queue in background on iOS

I am writing an iOS application using AudioQueue for recording. I am creating an input queue configured to receive linear PCM, declared this queue, and everything works as expected.

To control interrupts, I implemented the AVAudioSession delegate methods to catch the start and end of an interrupt. The endInterruption method is as follows:

- (void)endInterruptionWithFlags:(NSUInteger)flags; { if (flags == AVAudioSessionInterruptionFlags_ShouldResume && audioQueue != 0) { NSLog(@"Current audio session - category: '%@' mode: '%@'", [[AVAudioSession sharedInstance] category], [[AVAudioSession sharedInstance] mode]); NSError *error = nil; OSStatus errorStatus; if ((errorStatus = AudioSessionSetActive(true)) != noErr) { error = [self errorForAudioSessionServiceWithOSStatus:errorStatus]; NSLog(@"Could not reactivate the audio session: %@", [error localizedDescription]); } else { if ((errorStatus = AudioQueueStart(audioQueue, NULL)) != noErr) { error = [self errorForAudioQueueServiceWithOSStatus:errorStatus]; NSLog(@"Could not restart the audio queue: %@", [error localizedDescription]); } } } // ... } 

If the application is interrupted when it is in the foreground, everything works correctly. The problem occurs if the interrupt occurs in the background. Activating an audio session will result in an error ! Cat :

The specified audio session category cannot be used to attempt to play audio. For example, you tried to play or record audio with the audio session category set to kAudioSessionCategory_AudioProcessing.

Running a queue without activating a session results in an error: -12985

At this point, the category is set to AVAudioSessionCategoryPlayAndRecord , and the mode is AVAudioSessionModeDefault .

I could not find the documentation for this error message, and I can also restart the input audio queue in the background.

+7
source share
3 answers

It is currently not possible to activate if you are in the background.

0
source

Yes, it is possible, but to re-enable the session in the background, the audio session must either set AudioSessionProperty kAudioSessionProperty_OverrideCategoryMixWithOthers

 OSStatus propertySetError = 0; UInt32 allowMixing = true; propertySetError = AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing ); 

or the application should receive remote control command events :

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; 
+7
source

Have you created support for your application in info.plist? I'm not sure if recording is possible in the background, but you probably need to add “Required background modes” and then the value in this array is “Application plays audio”

The update I just checked and recording in the background is possible.

0
source

All Articles