【问题标题】:WebRTC iOS: Record Remote Audio stream using WebRTCWebRTC iOS:使用 WebRTC 录制远程音频流
【发布时间】:2020-01-09 15:40:57
【问题描述】:

我正在开发一个带有接收器录音功能的音频流应用程序。

我卡在了用户想要在接收端录制音频流的地方。

下面是我的代码 初始化

var engine = AVAudioEngine()
var recordingFile: AVAudioFile?
var audioPlayer: AVAudioPlayer?
let player = AVAudioPlayerNode()
var isRecording: Bool = false

初始化音频引擎

func initializeAudioEngine() {

        let input = self.engine.inputNode
        let format = input.inputFormat(forBus: 0)

        self.engine.attach(self.player)
        let mainMixerNode = self.engine.mainMixerNode
        self.engine.connect(input, to:mainMixerNode, format: format)
        self.engine.prepare()

        do {
            try self.engine.start()
            self.startRecording()
        } catch (let error) {
            print("START FAILED", error)
        }
    }

开始录制

func startRecording() {

        self.createRecordingFile()

        self.engine.mainMixerNode.installTap(onBus: 0,
                                         bufferSize: 1024,
                                         format: self.engine.mainMixerNode.outputFormat(forBus: 0)) { (buffer, time) -> Void in
            do {
                self.isRecording = true
                try self.recordingFile?.write(from: buffer)
            } catch (let error) {
                print("RECORD ERROR", error);
            }
            return
        }
    }

创建缓冲区

private func createBuffer(forFileNamed fileName: String) -> AVAudioPCMBuffer? {
        var res: AVAudioPCMBuffer?

        if let fileURL = Bundle.main.url(forResource: fileName, withExtension: "caf") {
            do {
                let file = try AVAudioFile(forReading: fileURL)
                res = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity:AVAudioFrameCount(file.length))
                if let _ = res {
                    do {
                        try file.read(into: res!)
                    } catch (let error) {
                        print("ERROR read file", error)
                    }
                }
            } catch (let error) {
                print("ERROR file creation", error)
            }
        }
        return res
    }

停止录制

func stopRecording() {
   self.engine.mainMixerNode.removeTap(onBus: 0)
}

我正在尝试使用耳机录音,但它不起作用

【问题讨论】:

  • WebRTC 在接收端不提供任何录制选项。
  • 请与我们分享您尝试过的和无效的。还可以考虑重新提出您的问题。 “谁能帮忙?”不是right way of asking
  • @normanius 我尝试使用 AVAudioEngine 从耳机录制声音,但它不起作用

标签: ios objective-c swift webrtc


【解决方案1】:

它会起作用,因为一旦你设置好了

let audiosession = AVAudioSession()

作为AVAudioSessionCategoryPlayAndRecord并设置

audiosession.setActive(true)

它将开始将任何音频转储记录到设备。

【讨论】:

    【解决方案2】:

    WebRTC 没有任何内部 API 来开始或停止录制。 我们可以尝试改用 AVAudioSession

    首次设置音频会话

    func setUPAudioSession() -> Bool {
          let audiosession = AVAudioSession()
          do {
            try audiosession.setCategory(AVAudioSessionCategoryPlayAndRecord)
          } catch(let error) {
             print("--> \(error.localizedDescription)")
          }
         do {
            try audiosession.setActive(true)
          } catch (let error) {
            print("--> \(error.localizedDescription)")
          }
            return audiosession.isInputAvailable;
    }
    

    设置音频会话后,现在开始录制如下

    func startRecording() -> Bool {
          var settings: [String: Any]  = [String: String]()
              settings[AVFormatIDKey] = kAudioFormatLinearPCM
              settings[AVSampleRateKey] = 8000.0
              settings[AVNumberOfChannelsKey] = 1
              settings[AVLinearPCMBitDepthKey] = 16
              settings[AVLinearPCMIsBigEndianKey] = false
              settings[AVLinearPCMIsFloatKey] = false
              settings[AVAudioQualityMax] = AVEncoderAudioQualityKey
        //Create device directory where recorded file will be save   automatically
               let searchPaths: [String] =               NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true)
             let documentPath_ = searchPaths.first
             let pathToSave = "\(documentPath_)/\(dateString)"
             let url: URL = URL(pathToSave)
             recorder = try? AVAudioRecorder(url: url, settings: settings)
             // Initialize degate, metering, etc.
             recorder.delegate = self;
             recorder.meteringEnabled = true;
             recorder?.prepareToRecord()
             if let recordIs = recorder {
                 return recordIs.record()
             }
             return false
      }
    

    播放录音文件

    func playrecodingFile() {
        //Get the path of recorded file saved in previous method
        let searchPaths: [String] =    NSSearchPathForDirectoriesInDomains(.documentDirectory, .allDomainsMask, true)
         let documentPath_ = searchPaths.first
         let fileManager = FileManager.default
         let arrayListOfRecordSound: [String]
          if fileManager.fileExists(atPath: recordingFolder()) {
              let arrayListOfRecordSound = try?     fileManager.contentsOfDirectory(atPath: documentPath_)
          }
      let selectedSound = "\(documentPath_)/\(arrayListOfRecordSound.first)"
      let url = URL.init(fileURLWithPath: selectedSound)
      let player = try? AVAudioPlayer(contentsOf: url)
          player?.delegate = self;
          try?  AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
          player?.prepareToPlay()
          player?.play()
    }
    

    停止录制

    func stopRecording() {
       recorder?.stop()
     }
    

    暂停录制

    func pauseRecording() {
        recorder?.pause()
    }
    

    停止录制

    func stopRecording() {
        recorder?.stop()
    }
    

    【讨论】:

    • 这行不通,因为 AVAudioRecorder 记录的是我们说的内容,而不是我们正在听的内容,我想记录我们从服务器端收听的内容
    • 它会起作用,因为一旦你设置 let audiosession = AVAudioSession() As AVAudioSessionCategoryPlayAndRecord 并设置 audiosession.setActive(true) 它将开始录制任何音频转储到设备。跨度>
    • 它录制音频,我们在扬声器上说话,而不是从耳机,仅供参考:AVAudioSession 在 WebRTC 中变为 RTCAVAudioSession,所以,AudioSession 已经存在
    • @Daljeet:谢谢它对我有用。我可以录制远程流以及本地音视频流。
    • @Hardik1344 我也做了同样的事情,但我可以听到重新编码的声音,我们说的是它记录的内容,而不是在 WebRTC 中记录 RTCAVAudioSession。如果你能详细说明
    猜你喜欢
    • 2017-06-27
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    相关资源
    最近更新 更多