I'm using AVAudioEngine
and I'm trying to get it to output .pcmFormatInt16
at 16000Hz but I can't seem to get it to work. Here's what I'm doing:
let audioEngine = AVAudioEngine()
let mixer = AVAudioMixerNode()
let input = self.audioEngine.inputNode!
audioEngine.attach(mixer)
audioEngine.connect(input, to: mixer, format: input.outputFormat(forBus: 0))
let recordingFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: true)
mixer.installTap(onBus: 0, bufferSize: 2048, format: recordingFormat) { [weak self] (buffer, _) in
// buffer here is all 0's!
}
self.audioEngine.prepare()
try! self.audioEngine.start()
As noted above, when I access the buffer it is always all 0, silence.
AVAudioEngine don't supports changing sample rate. You can use AVAudioConverter to change sample rate like that
let inputFormat = input.outputFormat(forBus: 0)
let recordingFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: true)
converter = AVAudioConverter(from: inputFormat, to: recordingFormat)
mixer.installTap(onBus: 0, bufferSize: 2048, format: inputFormat) { [weak self] (buffer, _) in
let convertedBuffer = self?.converter.convertBuffer(additionalBuffer: buffer)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.