ios – AVAudioRecorder在音频会话中断结束后不在后台录制

前端之家收集整理的这篇文章主要介绍了ios – AVAudioRecorder在音频会话中断结束后不在后台录制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在我的应用程序中录制音频,包括前景和后台.我还处理AVAudioSessionInterruptionNotification以在中断开始时停止录制并在结束时再次开始.虽然在前台它按预期工作,当应用程序在后台录制并且我接到一个呼叫时,它不会在呼叫结束后再次开始录制.我的代码如下:
- (void)p_handleAudioSessionInterruptionNotification:(NSNotification *)notification
        {
            NSUInteger interruptionType = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];

            if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
                if (self.isRecording && !self.interruptedWhileRecording) {

                    [self.recorder stop];

                    self.interruptedWhileRecording = YES;
                    return;
                }
            }

            if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
                if (self.interruptedWhileRecording) {
                    NSError *error = nil;
                    [[AVAudioSession sharedInstance] setActive:YES error:&error];

                    NSDictionary *settings = @{
                                       AVEncoderAudioQualityKey: @(AVAudioQualityMax),AVSampleRateKey: @8000,AVFormatIDKey: @(kAudioFormatLinearPCM),AVNumberOfChannelsKey: @1,AVLinearPCMBitDepthKey: @16,AVLinearPCMIsBigEndianKey: @NO,AVLinearPCMIsFloatKey: @NO
                                       };

                    _recorder = [[AVAudioRecorder alloc] initWithURL:fileURL settings:settings error:nil];

                    [self.recorder record];

                    self.interruptedWhileRecording = NO;
                    return;
                }
            }
        }

请注意,fileURL指向NSDocumentDirectory子目录中的新caf文件.配置背景模式音频.我也试过voip和沉默,都没有成功.

AVAudioSessionInterruptionTypeEnded块中的NSError是OSStatus错误560557684,我还没有找到解决方法.

任何帮助将非常感激.

解决方法

错误560557684适用于AVAudioSessionErrorCodeCannotInterruptOthers.当您的后台应用程序尝试激活不与其他音频会话混合的音频会话时,会发生这种情况.后台应用程序无法启动不与前台应用程序的音频会话混合的音频会话,因为这会中断当前用户正在使用的应用程序的音频.

解决此问题,请确保将会话类别设置为可混合的会话类别,例如AVAudioSessionCategoryPlayback.另外一定要设置类别选项AVAudioSessionCategoryOptionMixWithOthers(必需)和AVAudioSessionCategoryOptionDuckOthers(可选).例如:

// background audio *must* mix with other sessions (or setActive will fail)
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                 withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDuckOthers
                                       error:&sessionError];
if (sessionError) {
    NSLog(@"ERROR: setCategory %@",[sessionError localizedDescription]);
}

错误代码560557684实际上是32位整数中的4个ascii字符’!int’.错误代码列在AVAudioSession.h文件中(另请参见AVAudioSession):

@enum AVAudioSession error codes
    @abstract   These are the error codes returned from the AVAudioSession API.
...
    @constant   AVAudioSessionErrorCodeCannotInterruptOthers
        The app's audio session is non-mixable and trying to go active while in the background.
        This is allowed only when the app is the NowPlaying app.

typedef NS_ENUM(NSInteger,AVAudioSessionErrorCode)
{
...
    AVAudioSessionErrorCodeCannotInterruptOthers = '!int',/* 0x21696E74,560557684 */
...
原文链接:https://www.f2er.com/iOS/333034.html

猜你在找的iOS相关文章