无法初始化 AVAudioCompressedBuffer

Failure to initialize a AVAudioCompressedBuffer

我正在尝试使用以下格式初始化 AVAudioCompressedBuffer

<AVAudioFormat 0x2818944b0:  2 ch,  48000 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame>

像这样:

AVAudioCompressedBuffer* compressedBuffer = [[AVAudioCompressedBuffer alloc] initWithFormat:format 
                                                                             packetCapacity:NUM_AQ_BUFS];

但是我遇到了这个异常:

Thread 1: "required condition is false: maximumPacketSize != 0"

我错过了什么?

谢谢

您使用了错误的初始值设定项。 [AVAudioCompressedBuffer initWithFormat:packetCapacity:] 的头文件说

This fails if the format is PCM or if the format has variable bytes per packet (format.streamDescription->mBytesPerPacket == 0).

正如您从格式中看到的那样,您有 0 bytes/packet.

改用-[AVAudioCompressedBuffer packetCapacity:maximumPacketSize:]

AVAudioCompressedBuffer* compressedBuffer = [[AVAudioCompressedBuffer alloc] initWithFormat:format packetCapacity:NUM_AQ_BUFS maximumPacketSize: 1024];

tbh 我不知道 mp3 的最大数据包大小应该是多少。显然它是 144 个字节。您可以使用 AudioCodecGetPropertyInfo() + kAudioCodecPropertyMaximumPacketByteSize 查询或从您自己的数据包流中获取它。