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

iOS中WKWebView播放YouTube视频时摄像头与麦克风中断的解决需求

解决YouTube播放时摄像头与语音识别中断的问题

问题根源

YouTube视频播放时,WKWebView会抢占AVAudioSession并切换到纯播放模式,同时系统会因资源优先级暂停AVCaptureSession(摄像头)和SFSpeechRecognizer的音频捕获,导致两者停止工作。


解决方案

1. 配置兼容的AVAudioSession

修改音频会话初始化代码,设置支持同时播放与录制,并允许音频混合,避免WKWebView独占会话:

guard let node = audioEngine?.inputNode else { return }
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
    self.request.append(buffer)
}
audioEngine?.prepare()

do {
    let audioSession = AVAudioSession.sharedInstance()
    // 设置支持播放+录制,并允许混合其他音频
    try audioSession.setCategory(.playAndRecord, 
                                 mode: .default, 
                                 options: [.mixWithOthers, .defaultToSpeaker])
    try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
    try audioEngine?.start()
} catch {
    print("Audio session setup error: \(error)")
}

recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
    // 你的语音识别逻辑
})

2. 监听音频会话中断并恢复服务

添加AVAudioSession中断通知,在中断结束后重启音频引擎和语音识别:

// 在初始化方法中添加通知监听
NotificationCenter.default.addObserver(self, 
                                       selector: #selector(handleAudioSessionInterruption(_:)), 
                                       name: AVAudioSession.interruptionNotification, 
                                       object: nil)

// 实现中断处理方法
@objc private func handleAudioSessionInterruption(_ notification: Notification) {
    guard let userInfo = notification.userInfo else { return }
    
    if let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
       let type = AVAudioSession.InterruptionType(rawValue: typeValue) {
        
        if type == .began {
            // 中断开始,暂停音频引擎和识别任务
            audioEngine?.stop()
            recognitionTask?.cancel()
        } else if type == .ended {
            if let optionValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt,
               AVAudioSession.InterruptionOptions(rawValue: optionValue).contains(.shouldResume) {
                // 恢复音频引擎和语音识别
                do {
                    try audioEngine?.start()
                    restartSpeechRecognition()
                } catch {
                    print("Failed to restart audio engine: \(error)")
                }
            }
        }
    }
}

// 重启语音识别任务
private func restartSpeechRecognition() {
    recognitionTask?.cancel()
    recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
        // 复用原有的语音识别逻辑
    })
}

3. 监听摄像头会话中断并重启

添加AVCaptureSession中断通知,在中断结束后重启摄像头会话:

// 在初始化摄像头时添加通知监听
NotificationCenter.default.addObserver(self, 
                                       selector: #selector(sessionInterruptionEnded(_:)), 
                                       name: .AVCaptureSessionInterruptionEnded, 
                                       object: session)

NotificationCenter.default.addObserver(self, 
                                       selector: #selector(sessionWasInterrupted(_:)), 
                                       name: .AVCaptureSessionWasInterrupted, 
                                       object: session)

// 中断开始处理
@objc private func sessionWasInterrupted(_ notification: Notification) {
    // 可在此添加中断提示逻辑
}

// 中断结束后重启会话
@objc private func sessionInterruptionEnded(_ notification: Notification) {
    DispatchQueue.global(qos: .userInitiated).async {
        self.session?.startRunning()
    }
}

4. 配置WKWebView的媒体播放行为

创建WKWebView时配置允许内联播放,避免视频播放时抢占会话资源:

let webConfig = WKWebViewConfiguration()
webConfig.allowsInlineMediaPlayback = true
webConfig.mediaTypesRequiringUserActionForPlayback = .video // 仅需用户触发视频播放

var youTubePlayerWebView: WKWebView! = WKWebView(frame: CGRect(x: videoplayerX, y: videoplayerY, width: videoplayerWidth, height: videoplayerHeight), configuration: webConfig)
youTubePlayerWebView.load(URLRequest(url: videoUrl))
self.view.addSubview(youTubePlayerWebView)

5. 确认Info.plist权限

确保已添加必要的隐私权限描述:

  • NSCameraUsageDescription:摄像头使用说明
  • NSMicrophoneUsageDescription:麦克风使用说明
  • NSSpeechRecognitionUsageDescription:语音识别使用说明

内容的提问来源于stack exchange,提问作者Zhang Zhan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 15:47:08