hello,
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=[[exporter supportedFileTypes] objectAtIndex:0];
[exporter exportAsynchronouslyWithCompletionHandler:^{
}];
Thank you in advance !