1
我一直在嘗試使用AVMutableComposition將音頻(錄製)與視頻(.mp4)混合,這意味着我需要2個文件在混合後並行播放,這裏是代碼i使用: temporaryRecFile正在錄製的文件URL路徑 audiosURL是視頻文件的URL路徑AVMutableComposition刪除視頻音軌
NSURL *audiosURL =[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"dam3.mp4"] ofType:nil]];
NSLog(@"SOMEDATA IS THERE ");
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:temporaryRecFile options:nil];
AVURLAsset* audio2Asset = [[AVURLAsset alloc]initWithURL:audiosURL options:nil];
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentary2Track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentary2Track insertTimeRange:CMTimeRangeMake(kCMTimeZero, audio2Asset.duration) ofTrack:[[audio2Asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
NSLog(@"audio =%@",audioAsset);
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];
NSString* videoName = @"export.mov";
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
{
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
//@"com.apple.quicktime-movie";
NSLog(@"file type %@",_assetExport.outputFileType);
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void)
{
switch (_assetExport.status)
{
case AVAssetExportSessionStatusCompleted:
// export complete
NSLog(@"Export Complete");
// From Here I want play movie using MPMoviePlayerController.<<<---------
NSString *fileNamePath = @"sound_record.mov";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:oldappSettingsPath]) {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath: oldappSettingsPath error:NULL];
}
NSURL *documentDirectoryURL = [NSURL fileURLWithPath:oldappSettingsPath];
[[NSFileManager defaultManager] copyItemAtURL:exportUrl toURL:documentDirectoryURL error:nil];
[audioAsset release];
[audio2Asset release];
[_assetExport release];
[self performSelectorOnMainThread:@selector(playVideo:) withObject:documentDirectoryURL waitUntilDone:NO];
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Export Failed");
NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);
// export error (see exportSession.error)
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export Failed");
NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);
// export cancelled
break;
}
}
];
其結果是,導出的視頻播放錄製的音頻與視頻成功,但視頻中的音軌是已刪除,因此導出的.mov正在播放帶有視頻圖像的錄製音頻文件而沒有其聲音,爲什麼會發生這種情況?