0

I'm using AVAssetReaderTrackOutput to music raw data from iPod library, racing WAV or MP3 file format from iPod library work fine. However, if i read M4A file i will get data with all 0s or garbage... The M4A file i'm reading is playable and i'm sure is not encrypted... It seems to be iOS encrypt or output 0s when you are reading M4A file. Does anyone has any suggestion on how to resolve this issue?

NOTE: If i use AVAssetExportSession to export from m4a file to m4a file then i get correct raw data but this means i have to first save it to a disk (on Apps document folder for example) and then reading it. I'd like to be able to stream it so i need to find a way to read it directly.

Here is my code and i get correct mp3 or wav raw data "except m4a":

NSURL *assetURL = [MediaItem valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

AVAssetTrack* songTrack = [songAsset.tracks objectAtIndex:0];

AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[songAsset.tracks objectAtIndex:0] outputSettings:nil];

NSError *assetError = nil;
AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:songAsset
                                                       error:&assetError];


[assetReader addOutput: assetReaderOutput];                

//Retain
self.mStreamAssetReader = assetReader;
self.mStreamAssetReaderOutput = assetReaderOutput;

1 Answers1

0

Assuming that by "raw music data" you mean raw PCM audio, you should pass in an initialised NSDictionary into your AVAssetReaderTrackOutput with the appropriate parameters such that iOS will decode the AAC bitstream inside the .m4a file for you.

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
    [NSNumber numberWithFloat:48000.0], AVSampleRateKey,
    [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
    [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
    nil];

AVAssetReaderOutput *assetReaderOutput =
    [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[songAsset.tracks objectAtIndex:0]
                                               outputSettings:settings];
Chris McGrath
  • 1,936
  • 16
  • 17