我使用AVAudioRecorder創建和存儲AAC編碼的.m4a文件。這產生了一個可播放的.m4a文件。然後,我想使用AVAssetExportSession處理文件,以便將元數據添加到文件中。下面的代碼是生成一個類似大小的.m4a文件(比源文件少1 KB),但是當它播放時,就會有沉默。通過AVAssetExportSession向AAC M4A添加元數據
NSURL* url = [NSURL fileURLWithPath:self.m4aPath];
AVURLAsset* asset = [AVAsset assetWithURL:url];
AVMutableMetadataItem* t = [AVMutableMetadataItem metadataItem];
t.key = AVMetadataCommonKeyTitle;
t.keySpace = AVMetadataKeySpaceCommon;
t.value = @"Unit Test";
NSArray* metadata = [NSArray arrayWithObject:t];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
exportSession.outputURL = [NSURL fileURLWithPath:[[NSFileManager rawRecordingsDirectory] stringByAppendingPathComponent:@"test.m4a"]];
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.metadata = metadata;
[exportSession exportAsynchronouslyWithCompletionHandler:^{....}];
還有一個一塊信息:當我看在源和在Finder中導出的文件,源文件有黑色的iTunes圖標,而導出的文件有白色的iTunes圖標。不確定這在實踐中意味着什麼,但希望它可能有所幫助。此外,雙擊源將其添加到iTunes並開始播放,而雙擊導出後會打開iTunes,但什麼也不做。