You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Zoom等应用共享屏幕时AVAudioRecorder录音失败求助

问题解决:屏幕共享时AVAudioSession激活失败(错误码561017449)

问题原因

当Zoom等应用开启屏幕共享时,会抢占系统音频会话并设置为独占模式,导致你的App无法激活playAndRecord类别的音频会话,触发NSOSStatusErrorDomain Code=561017449错误。

解决方案

1. 优化音频会话激活逻辑

激活会话时添加选项,允许通知其他应用音频会话状态变化,同时针对性处理激活失败场景:

UIApplication.shared.beginReceivingRemoteControlEvents()
let session = AVAudioSession.sharedInstance()
do {
    // 调整类别选项,添加mixWithOthers允许音频混合(按需选择)
    try session.setCategory(.playAndRecord, options: [.defaultToSpeaker, .mixWithOthers])
    // 激活会话时携带通知其他应用的选项
    try session.setActive(true, options: .notifyOthersOnDeactivation)
    
    let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 44100,
        AVNumberOfChannelsKey: 2,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]
    audioRecorder = try AVAudioRecorder(url: getFileUrl(), settings: settings)
    audioRecorder.delegate = self
    audioRecorder.isMeteringEnabled = true
    audioRecorder.prepareToRecord()
    self.nextBtn.isHidden = true
} catch let error as NSError {
    print("Error \(error)")
    // 针对目标错误码,弹窗提示用户
    if error.code == 561017449 {
        let alert = UIAlertController(title: "录音失败", message: "当前有应用正在屏幕共享,请关闭后重试", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true)
    }
}

2. 监听音频会话中断通知

添加监听,当屏幕共享结束(音频会话中断恢复)时自动重试激活:

// 在视图初始化或viewDidLoad中添加监听
NotificationCenter.default.addObserver(self, selector: #selector(handleAudioSessionInterruption(_:)), name: AVAudioSession.interruptionNotification, object: nil)

// 中断处理方法
@objc func handleAudioSessionInterruption(_ notification: Notification) {
    guard let info = notification.userInfo,
          let typeValue = info[AVAudioSessionInterruptionTypeKey] as? UInt,
          let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
        return
    }
    
    if type == .ended {
        guard let optionsValue = info[AVAudioSessionInterruptionOptionKey] as? UInt else { return }
        let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
        if options.contains(.shouldResume) {
            let session = AVAudioSession.sharedInstance()
            do {
                try session.setActive(true)
                audioRecorder?.record() // 按需重新启动录音
            } catch {
                print("重新激活会话失败: \(error)")
            }
        }
    }
}

3. 前置音频占用检查

在触发录音前,先检查是否有其他应用占用音频会话:

let session = AVAudioSession.sharedInstance()
if session.isOtherAudioPlaying {
    let alert = UIAlertController(title: "提示", message: "当前有应用正在使用音频,请关闭后重试", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "确定", style: .default))
    present(alert, animated: true)
    return
}
// 后续执行激活会话和录音逻辑

内容的提问来源于stack exchange,提问作者iOS Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:25:23