Swift 3 AVAudioEngine set microphone input format

馋奶兔 提交于 2019-12-06 10:50:03

Oh my god, I think I got it. I was too blind to see that you can specify the format of the installTap callback. This seems to work

let audioEngine  = AVAudioEngine()

func startRecording() {
    let inputNode = audioEngine.inputNode!
    let bus = 0

    let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: false)

    inputNode.installTap(onBus: bus, bufferSize: 2048, format: format) { // inputNode.inputFormat(forBus: bus)
        (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in

        let values = UnsafeBufferPointer(start: buffer.int16ChannelData![0], count: Int(buffer.frameLength))
        let arr = Array(values)
        print(arr)
    }


    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch {
        print("Error info: \(error)")
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!