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.


目前,如果您处于后台,则无法重新激活。


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.

链接地址: http://www.djcxy.com/p/79026.html

上一篇: 核心音频(音频单元)音频会话和MPVolumeView

下一篇: 无法在iOS上以后台模式重新启动中断的音频输入队列