我有一个问题AVAudioConverter
。我正在尝试转换AVAudioPCMBuffers
为AAC
.
这是我的转换函数,取自WWDC 2015的“Core Audio 中的新功能” 会话 507。
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)
}
问题是 outBuffer 是空的。如果我打印它,我会得到输出<AVAudioCompressedBuffer@0x170012420: 0/6144 bytes>
,所以你可以看到它没有被填充。
如果您想知道我是AVAudioPCMBuffer
通过在AVAudioEngine
. 我正在转换为与inputFormat
使用AVAudioEngines
主混音器相同的格式。
有谁知道解决这个问题?