简体   繁体   English

Swift AVAudioEngine - 如何使本地麦克风静音

[英]Swift AVAudioEngine - How to mute local mic

I'm using AVAudioEngine to get local mic input and then broadcast it.我正在使用AVAudioEngine获取本地麦克风输入,然后进行广播。 I want to stop getting local mic input when mute button is tapped.我想在点击静音按钮时停止获取本地麦克风输入。

I tried three things:我尝试了三件事:

  1. audioEngine.inputNode.isVoiceProcessingInputMuted = true - It did not work. audioEngine.inputNode.isVoiceProcessingInputMuted = true - 它不起作用。
  2. audioEngine.stop() - Actually it works but causes other problems: I'm using HaishinKit to stream screen-recording + mic-input + other device audio. audioEngine.stop() - 实际上它可以工作,但会导致其他问题:我正在使用HaishinKit到 stream 屏幕录制 + 麦克风输入 + 其他设备音频。 Stopping the audioEngine causes small lag on viewer side.停止 audioEngine 会导致查看器端出现小延迟。
  3. Mute using HaishinKit.RTMPStream : rtmpStream.audioSettings[.muted] = true - It works but mutes entire audio.使用HaishinKit.RTMPStream静音: rtmpStream.audioSettings[.muted] = true - 它可以工作,但会静音整个音频。 I want to mute only local mic not other device audio.我只想静音本地麦克风而不是其他设备音频。

// //

private func startTappingMicrophone() {
    let inputNode = audioEngine.inputNode
    let inputFormat = inputNode.outputFormat(forBus: 0)
    audioEngine.inputNode.installTap(onBus: 0, bufferSize: AVAudioFrameCount(bufferSize), format: inputFormat) { [weak self] buffer, _ in
          guard let self = self else { return }
          self.conversionQueue.async {
            if let cmSampleBuffer = self.configureSampleBuffer(pcmBuffer: buffer) {
              self.delegate?.recievedRecordedFrame(cmSampleBuffer)
            }
          }
    }
    audioEngine.prepare()
    try? audioEngine.start()
}

Depending on your setup, it may help to simply not send buffers to the delegate.根据您的设置,简单地不向委托发送缓冲区可能会有所帮助。

But what will work either way is to zero the buffer:但是无论哪种方式都可以将缓冲区归零:

if let floatChannelData = buffer.floatChannelData {
    for c in 0..<Int(buffer.format.channelCount) {
        memset(floatChannelData[c], 0, MemoryLayout<Float>.size * Int(buffer.frameLength))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM