我想用AVFoundation将输入节点下采样到8000采样率

时间:2016-11-25 21:42:26

标签: ios swift3 avfoundation audioformat

我有以下代码尝试采样率8000 我怎样才能下采样率? 我找到了解决方案,但它对我不起作用!

这就是解决方案......

    assert(audioEngine.inputNode != nil)
    let input = audioEngine.inputNode!

    let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)
    let mixer = AVAudioMixerNode()
   // var mainMixerNode: AVAudioMixerNode { get }
    audioEngine.attach(mixer)
    audioEngine.connect(input, to: mixer, format: input.inputFormat(forBus: 0))
    mixer.volume = 0

    audioEngine.connect(mixer, to: audioEngine.mainMixerNode, format: audioFormat)

    do {
        try audioEngine.start()

        mixer.installTap(onBus: 0, bufferSize: 1024, format: audioFormat, block: {
            (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
            //some code here
            print(buffer)

            let firstChannel = buffer.floatChannelData?[0]

            //UnsafeMutablePointer
            let arr = Array(UnsafeBufferPointer(start: firstChannel, count: Int(buffer.frameLength)))

            self.array.append(contentsOf: arr)

            print(self.array)


        })

    } catch let error {
        print(error.localizedDescription)
    }

但在我的self.array总是null = 0

我做错了什么?

0 个答案:

没有答案