Failure to initialize a AVAudioCompressedBuffer

122 Views Asked by At

I'm trying to initialize an AVAudioCompressedBuffer using this format:

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

like such:

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

But I'm getting this exception:

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

What am I missing?

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

You're using the wrong initializer. The header file for [AVAudioCompressedBuffer initWithFormat:packetCapacity:] says

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

and as you can see from the format, you have 0 bytes/packet.

Use -[AVAudioCompressedBuffer packetCapacity:maximumPacketSize:] instead

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

tbh I don't know what the maximum packet size for mp3 should be. Apparently it's 144 bytes. You can query this with AudioCodecGetPropertyInfo() + kAudioCodecPropertyMaximumPacketByteSize or derive it from your own packet stream.