首页 > 解决方案 > WebRTC iOS:如何录制远程音频流?

问题描述

用例是我正在运行 webRTC 会话并尝试捕获远程和本地音轨。使用 AVAudioSession 和 AVAudioRecorder 我只能录制通过麦克风进入的本地视频轨道。通过扬声器传来的远程音轨根本没有被记录下来。这是我尝试过的示例。

func setupView() {
    recordingSession = AVAudioSession.sharedInstance()
    do {
        try recordingSession.setCategory(.playAndRecord, options: .defaultToSpeaker)
        try recordingSession.setMode(.videoChat)
        try recordingSession.setActive(true)
        recordingSession.requestRecordPermission() { [unowned self] allowed in
            DispatchQueue.main.async {
                if allowed {
                    //self.loadRecordingUI()
                } else {
                    // failed to record
                }
            }
        }
    } catch {
        // failed to record
    }
}

@IBAction func recordButtonTapped() {
    if audioRecorder == nil {
        startRecording()
    } else {
        finishRecording(success: true)
    }
}

func startRecording() {
    let audioFilename = getFileURL()
    
    let recordSettings = [ AVFormatIDKey: Int(kAudioFormatLinearPCM),
                           AVSampleRateKey: 8000.0,
                           AVNumberOfChannelsKey: 1,
                           AVLinearPCMBitDepthKey: 16,
                           AVLinearPCMIsBigEndianKey: false,
                           AVLinearPCMIsFloatKey: false,
                           AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue] as [String : Any]
    
    do {
        audioRecorder = try AVAudioRecorder(url: audioFilename, settings: recordSettings)
        audioRecorder.delegate = self
        audioRecorder.record()
    } catch {
           finishRecording(success: false)
    }
}

关于我可能做错了什么的任何建议。

标签: iosswiftwebrtc

解决方案


推荐阅读