.m4a raw data from iPod Library not playing

我怕爱的太早我们不能终老 提交于 2019-12-05 21:58:45

I ran into the same problem. AVAssetReader returns only the raw audio data. It works for .mp3 because the file format is just a bunch of frames that contain everything a player needs to playback the file. You may note that all of the id3 information is missing in the exported file.

.m4a audio files are containers that wrap around aac audio data. You received only the audio data, without further information i.e. samplerate or the seek-table.

A workaround for your problem could be to use an AVAssetExportSession to export the AVAssetTrack to a file on the phone and stream the file from there. I tried exactly this and a working .m4a file was written.

The following code is based on Apples example how to export a trimmed audio asset:

// create the export session
AVAssetExportSession *exportSession = [AVAssetExportSession
                                       exportSessionWithAsset: songAsset
                                       presetName:AVAssetExportPresetAppleM4A];
if (nil == exportSession)
    NSLog(@"Error");

// configure export session  output with all our parameters
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *filePath = [NSString stringWithFormat: @"%@/export.m4a", basePath];

exportSession.outputURL = [NSURL fileURLWithPath: filePath]; // output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    if (AVAssetExportSessionStatusCompleted == exportSession.status)
    {
        NSLog(@"AVAssetExportSessionStatusCompleted");
    }
    else if (AVAssetExportSessionStatusFailed == exportSession.status)
    {
        // a failure may happen because of an event out of your control
        // for example, an interruption like a phone call comming in
        // make sure and handle this case appropriately
        NSLog(@"AVAssetExportSessionStatusFailed");
    }
    else
    {
        NSLog(@"Export Session Status: %d", exportSession.status);
    }
}];

The downside of this solution is, that the while file had to be written on the disk. I'm still searching for a better way to handle .m4a files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!