AVAssetExportSession - AVMutableCompositionTrack - exported output file is empty
I try to add a .mp3-file to an AVMutableCompositionTrack and after that I want to export the new file. The Problem is: The generated file exists after exporting but it is empty and can not be played. Does someone see the error in my code?
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=[[exporte开发者_如何学编程r supportedFileTypes] objectAtIndex:0];
[exporter exportAsynchronouslyWithCompletionHandler:^{
}];
Thank you in advance !
Like I wrote in the comments it had something to do with the different file formats. I changed my files to .m4a and also the code - so everything (the sources and the targets of this operation) is .m4a related and it works.
by the way: I tried also working with .wav-files, but there are strange things happening while the operations with wav. I don't recommend it.
精彩评论