我正在使用AVAssetReaderTrackOutput从iPod库中获取音乐原始数据,从iPod库中播放WAV或MP3文件格式工作正常。但是,如果我读取M4A文件,我将获得所有0或垃圾的数据...我正在阅读的M4A文件是可播放的,我确定没有加密...它似乎是iOS加密或输出0s当你正在阅读M4A文件。有没有人对如何解决这个问题有任何建议?
注意:如果我使用AVAssetExportSession从m4a文件导出到m4a文件,那么我得到正确的原始数据,但这意味着我必须先将其保存到磁盘(例如,在Apps文档文件夹中)然后读取它。我希望能够流式传输它,所以我需要找到一种直接读取它的方法。
这是我的代码,我得到正确的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 :(得分: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];