3

我有一个问题AVAudioConverter。我正在尝试转换AVAudioPCMBuffersAAC.

这是我的转换函数,取自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主混音器相同的格式。

有谁知道解决这个问题?

4

1 回答 1

0

我认为问题在于inputFormat你构造的不同于inBuffer.format. 试试这个:

let inputFormat = inBuffer.format

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

于 2016-11-04T12:11:57.003 回答