在WatchOS上使用AVAudioEngine录制时阻止播放



我在iOS中使用AVAudioEngine从麦克风收集PCM数据,效果很好,但当我尝试将项目转移到WatchOS时,我在录制时得到了反馈。录制时如何停止扬声器播放?

var audioEngine = AVAudioEngine()
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
let input = audioEngine.inputNode
let format = input.inputFormat(forBus: 0)
audioEngine.connect(input, to: audioEngine.mainMixerNode, format: format)
try! audioEngine.start()
let mixer = audioEngine.mainMixerNode
let format = mixer.outputFormat(forBus: 0)
let sampleRate = format.sampleRate
let fft_size = 2048
mixer.installTap(onBus: 0, bufferSize: UInt32(fft_size), format: format, 
block: {(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
// Processing
}

对于其他遇到这种情况的人,我通过删除从inputNode到mainMixerNode的连接来修复它,并将tap直接安装在inputNode上。我想我之前做这件事的方式会产生一个反馈循环,在那里它会播放它正在录制的内容。不确定为什么这种情况只发生在WatchOS中而不发生在iPhone上。。。也许它是从耳机扬声器而不是麦克风旁边的扬声器播放的。固定代码:

var audioEngine = AVAudioEngine()
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
try! audioEngine.start()
let input = audioEngine.inputNode
let format = mixer.outputFormat(forBus: 0)
let sampleRate = format.sampleRate
let fft_size = 2048
input.installTap(onBus: 0, bufferSize: UInt32(fft_size), format: format, 
block: {(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
// Processing
}

相关内容

  • 没有找到相关文章

最新更新