我有以下代码尝试采样率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
我做错了什么?