2012-01-16 93 views
0

我正在使用AVAssetReaderTrackOutput从iPod库中的音乐原始数据,从iPod库中比赛WAV或MP3文件格式工作正常。但是,如果我阅读M4A文件,我将获得数据全0或垃圾...我正在阅读的M4A文件是可播放的,我确定没有加密...它似乎是iOS加密或输出0时,当你正在读取M4A文件。有没有人有任何建议如何解决这个问题?从iPod库中读取.m4a原始数据

注意:如果我使用AVAssetExportSession从m4a文件导出到m4a文件,那么我会得到正确的原始数据,但这意味着我必须先将它保存到磁盘(例如在应用程序文档文件夹中),然后读取它。我希望能够流式传输,所以我需要找到一种直接阅读的方式。

这里是我的代码,我得到正确的MP3或WAV原始数据“除了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; 

回答

0

假设由“原始音乐数据”你的意思是原始的PCM音频,你应该通过在初始化的NSDictionary放入AVAssetReaderTrackOutput中,并使用适当的参数,iOS将为您解码.m4a文件中的AAC比特流。

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];