AVAudioConverter将AVAudioPCMBuffer转换为AAC

时间:2016-11-04 09:37:27

标签: swift swift3 avfoundation aac avaudioconverter

我遇到了"<your device ip address>:<port>"的问题。我正在尝试将AVAudioConverter转换为AVAudioPCMBuffers

这是我的转换功能,取自 2015年WWDC 会话507 “新增的核心音频”

AAC

问题是outBuffer是空的。如果我打印它,我得到输出func convertPCMBufferToAAC(inBuffer : AVAudioPCMBuffer) -> Void { let inputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false) var outDesc = AudioStreamBasicDescription(mSampleRate: 8000, mFormatID: kAudioFormatMPEG4AAC, mFormatFlags: 0, mBytesPerPacket: 0, mFramesPerPacket: 0, mBytesPerFrame: 0, mChannelsPerFrame: 1, mBitsPerChannel: 0, mReserved: 0) let outputFormat = AVAudioFormat(streamDescription: &outDesc) let converter = AVAudioConverter(from: inputFormat, to: outputFormat) let outBuffer = AVAudioCompressedBuffer(format: outputFormat, packetCapacity: 8, maximumPacketSize: converter.maximumOutputPacketSize) let inputBlock : AVAudioConverterInputBlock = { inNumPackets, outStatus in outStatus.pointee = AVAudioConverterInputStatus.haveData return inBuffer } var error : NSError? let status = converter.convert(to: outBuffer, error: &error, withInputFrom: inputBlock) } ,因为你可以看到它没有填充。

如果您想知道我在<AVAudioCompressedBuffer@0x170012420: 0/6144 bytes>上安装点按AVAudioPCMBuffer。我使用AVAudioEngine主混音器转换为与inputFormat相同的格式。

有谁知道这个解决方案?

1 个答案:

答案 0 :(得分:0)

我认为问题在于您构建的inputFormatinBuffer.format不同。试试这个:

let inputFormat = inBuffer.format

并确保在设备上运行代码,因为它似乎无法在模拟器上运行。