Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not restart an interrupted audio input queue in background mode on iOS

I'm writing an iOS App using an AudioQueue for recording. I create an input queue configured to get linear PCM, stated this queue and everything works as expected.

To manage interruptions, I implemented the delegate methods of AVAudioSession to catch the begin and the end of an interruption. The method endInterruption looks like the following:

- (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 app gets interrupted while it is in foreground, everything works correct. The problem appears, if the interruption happens in the background. Activating the audio session result in the error !cat:

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

Starting the queue without activating the session results in the error code: -12985

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

I couldn't find any documentation for this error message, nor if it is possible to restart an input audio queue in the background.

like image 868
Tobias Kräntzer Avatar asked Feb 14 '12 12:02

Tobias Kräntzer


1 Answers

Yes it is possible, but to reactivate the session in the background, the audio session has to either set AudioSessionProperty kAudioSessionProperty_OverrideCategoryMixWithOthers

OSStatus propertySetError = 0;
    UInt32 allowMixing = true;



    propertySetError = AudioSessionSetProperty (
                                                kAudioSessionProperty_OverrideCategoryMixWithOthers,
                                                sizeof (allowMixing),
                                                &allowMixing
                                                );

or the app has to receive remote control command events:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
like image 85
nekiTamoTip Avatar answered Oct 01 '22 08:10

nekiTamoTip