我尝试将 .mp3 文件添加到 AVMutableCompositionTrack,然后我想导出新文件。问题是:导出后生成的文件存在,但为空,无法播放。有人在我的代码中看到错误吗?
AVMutableComposition *saveComposition = [AVMutableComposition composition];
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *tempPath = [docpaths objectAtIndex:0];
NSLog(@"Temporary Path: %@", tempPath);
NSString *audioPath = [[NSBundle mainBundle] pathForResource: @"1" ofType: @"mp3"];
NSURL *audioUrl = [[NSURL alloc] initFileURLWithPath:audioPath];
AVURLAsset *audio = [AVURLAsset URLAssetWithURL:audioUrl options:nil];
NSLog(@"%@", audio);
[audioUrl release];
AVMutableCompositionTrack *compositionAudioTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipAudioTrack = [[audio tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio duration]) ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
NSString *path = [tempPath stringByAppendingPathComponent:@"mergedaudio.m4a"];
if([[NSFileManager defaultManager] fileExistsAtPath:path])
{
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];
AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetAppleM4A] autorelease];
exporter.outputURL=url;
[exporter setOutputFileType:@"com.apple.m4a-audio"];
NSLog(@"%@", [exporter supportedFileTypes]);
exporter.outputFileType=[[exporter supportedFileTypes] objectAtIndex:0];
[exporter exportAsynchronouslyWithCompletionHandler:^{
}];
先感谢您 !