【发布时间】:2018-04-19 07:01:58
【问题描述】:
为什么我使用 AVMutableCompositionTrack 合并音频和视频时没有声音?
NSArray * breakArr = [[[[_dic objectForKey:@"url"] lastObject] objectForKey:@"url"]componentsSeparatedByString:@"/"];
NSString *pathAudio = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[breakArr lastObject]];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathAudio] options:nil];
NSString *pathVideo = self.pathToMovie;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathVideo] options:nil];
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:kCMTimeZero error:nil];
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:kCMTimeZero error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetHighestQuality];
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 = @"com.apple.quicktime-movie";
_assetExport.outputFileType = AVFileTypeMPEG4;
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
// your completion code here
[[ModelessAlertView instance]closeAlert];
UISaveVideoAtPathToSavedPhotosAlbum(exportPath, self, @selector(videoWithPatch:didFinishSavingWithError:contextInfo:), nil);
}
];
这里是代码。 当我查看视频时。我只听到音轨的声音。而且我听不到视频声音。如何设置音量?
有人知道吗?非常感谢!
【问题讨论】:
-
因为您从未将视频音轨添加到您的作品中。
标签: ios objective-c audio video composition