Question

I am reading audio assets from the iPod library on iOS with an AVAssetReader and AVAssetReaderTrackOutput. However, when i read the data and put the chunks back together, the file isn't exactly the same. A few kB are missing, and therefore the audio file won't play back.

Here is the code i use to extract the audio data

// Copy next audio samples
CMSampleBufferRef buffer = [[reader_.outputs objectAtIndex:0] copyNextSampleBuffer];

// Extract bytes from buffer
CMBlockBufferRef dataBuffer = CMSampleBufferGetDataBuffer(buffer);

size_t bufLen = CMBlockBufferGetDataLength(dataBuffer);
UInt8 buf[bufLen];

CMBlockBufferCopyDataBytes(dataBuffer, 0, bufLen, buf);

// Pass data to delegate
if ([delegate respondsToSelector:@selector(assetStream:hasDataAvailable:)]) {
    [delegate assetStream:self hasDataAvailable:[NSData dataWithBytes:buf length:bufLen]];
}

// Invalidate buffer
CMSampleBufferInvalidate(buffer);

What am I doing wrong here?

Was it helpful?

Solution

Yep I guessed right. The copyNextSampleBuffer does omit the AAC header, and therefore the media player API can't process the file.

You can copy the stream description with other methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top