views:

145

answers:

1

I've tried to write the smallest chunk of code to narrow down a problem. It's now just a few lines and it doesn't work, which makes it pretty clear that I have a fundamental misunderstanding of how to use AudioFileReadPackets. I've read the docs and other examples online, and apparently I'm just not getting. Could you explain it to me?

Here's what this block should do: I've previously opened a file. I want to read just one packet - the first one of the file - and then print it. But it crashes on the AudioFileReadPackets line:

    AudioFileID mAudioFile2; 
    AudioFileOpenURL (audioFileURL, 0x01, 0, &mAudioFile2);
    UInt32 *audioData2 = (UInt32 *)malloc(sizeof(UInt32) * 1);
    AudioFileReadPackets(mAudioFile2, false, NULL, NULL, 0, (UInt32*)1, audioData2);
    NSLog(@"first packet:%i",audioData2[0]);

(For clarity, I've stripped out all error handling.)

It's the AFRP line that crashes out.

(I understand that the third and fourth argument are useful, and in my "real" code, I use them, but they're not required, right? So NULL in this case should work, right?) So then what's going on?

Any guidance would be much appreciated.

Thanks.

A: 

I think your problem is with the number of packets argument. You are just creating a pointer to 0x00000001. The problem is that it tries to use this is an output of how many packets were actually read. Your audio buffer might be too small too. According to the documentation, it's supposed to be the length of packets times the upper bound of the packet length.

Try this:

AudioFileID mAudioFile2; 
AudioFileOpenURL (audioFileURL, 0x01, 0, &mAudioFile2);
UInt32 packetCount = 1;
void *audioData2 = (void *)malloc(packetCount * maxPacketSize);
AudioFileReadPackets(mAudioFile2, false, NULL, NULL, 0, &packetCount, audioData2);
NSLog(@"first packet:%i",audioData2[0]);

Not really sure how to get the maxPacketSize (never worked with this), but hopefully this could help: http://3.ly/V4ty

Kevin Wiskia