录音被打断的一些处理

会话设置


- (void)setupAudioSession {
    self.audioSession = [AVAudioSession sharedInstance];
    NSError *error = nil;
    
    // 设置音频会话类别
    [self.audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
                     withOptions:AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker
                           error:&error];
    if (error) {
        [self handleError:error];
        return;
    }
    
    // 设置音频会话模式
    [self.audioSession setMode:AVAudioSessionModeDefault error:&error];
    if (error) {
        [self handleError:error];
        return;
    }
    
    // 激活音频会话
    [self.audioSession setActive:YES error:&error];
    if (error) {
        [self handleError:error];
        return;
    }
}

监听通知处理

- (void)registerForNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleInterruption:)
                                                 name:AVAudioSessionInterruptionNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleRouteChange:)
                                                 name:AVAudioSessionRouteChangeNotification
                                               object:nil];
}

- (void)handleInterruption:(NSNotification *)notification {
    NSDictionary *info = notification.userInfo;
    AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
    
    switch (type) {
        case AVAudioSessionInterruptionTypeBegan: {
            // 记录当前录音为打断状态 暂停当前录音

            break;
        }
        case AVAudioSessionInterruptionTypeEnded: {
            AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
            if (options & AVAudioSessionInterruptionOptionShouldResume) {
                // 如果之前在录音,此处尝试恢复录音
                
            }
            break;
        }
    }
}

- (void)handleRouteChange:(NSNotification *)notification {
    NSDictionary *info = notification.userInfo;
    AVAudioSessionRouteChangeReason reason = [info[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
    
    switch (reason) {
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: {
            // 音频路由发生变化(如插入/拔出耳机),重新配置音频会话
            [self setupAudioSession];
            break;
        }
        default:
            break;
    }
}

#pragma mark - Error Handling

- (void)handleError:(NSError *)error {
   //加一些处理 弹窗或者提示
}

结束录音时应该释放当前音频会话, 给别的应用去使用,一般来说都应该如此,不然很可能出现被打断恢复不了的情况,就算你监听了打断恢复的通知,没人发通知给你也是白瞎 ,
这句代码相当于发通知

  [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
posted @ 2024-12-22 11:54  CoderWGB  阅读(0)  评论(0编辑  收藏  举报