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

随声附和 提交于 2019-12-22 04:39:16

问题


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.


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/9276546/can-not-restart-an-interrupted-audio-input-queue-in-background-mode-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!