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.

有帮助吗?

解决方案

At the present there is no way to reactivate if you are in the background.

其他提示

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];

Have you made your app support backgrounding in the info.plist? I'm not sure if recording is possible in the background, but you probably need to add "Required Background Modes" and then a value in that array of "App plays audio"

Update I just checked and recording in the background is possible.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top