我有一个导出视频文件的代码。代码在iOS8上运行良好,但在iOS9设备上,视频文件没有声音。 我认为问题出在AVAssetExportSession上,但我无法解决问题并修复它。 同样奇怪的是,当我导出它进行播放时,资产的工作正常。
(void)export {
self.videoComposition.renderScale = 1.0;
self.videoComposition.animationTool = [self coreAnimationTool];
FakeAsset * assetFaker = [FakeAsset new];
[assetFaker fakeAssetWithDuration:self.currentTime completitionBlock:^(AVAsset *asset) {
[self addEmptyBackgroundTrackFromAsset:asset];
AVAssetExportSession * exportSession =
[AVAssetExportSession exportSessionWithAsset:[self.composition copy]
presetName:AVAssetExportPresetHighestQuality];
[MovieExport setCurrentExporter:exportSession];
NSUInteger i = 0;
NSString * path;
do {
path =
[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"movieExport%i.mov",i]]; i++;
} while ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:NULL]);
NSURL * url = [NSURL fileURLWithPath:path];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
}
exportSession.outputURL = url;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.audioMix = self.audioMix;
exportSession.videoComposition = self.videoComposition;
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, self.currentTime);
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusCompleted:
self.callback(@{MovieCreatorVideoURL : exportSession.outputURL}, nil);
break;
case AVAssetExportSessionStatusFailed:
self.callback(nil,exportSession.error);
break;
case AVAssetExportSessionStatusCancelled:
self.callback(nil, exportSession.error);
break;
default:
break;
}
}];
}];
}
- (void)exportForPlayback {
self.videoComposition.renderScale = 2.0;
FakeAsset * assetFaker = [FakeAsset new];
[assetFaker fakeAssetWithDuration:self.currentTime completitionBlock:^(AVAsset *asset){
[self addEmptyBackgroundTrackFromAsset:asset];
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:self.composition];
playerItem.videoComposition = self.videoComposition;
playerItem.audioMix = self.audioMix;
MovieMetadata * metaData = [MovieMetadata new];
metaData.orderedElements = self.orderedElements;
metaData.orderedMarkers = self.movieMarkers;
metaData.elementBeginTimes = self.elementBeginTimes;
metaData.filmSize = self.filmSize;
metaData.filmFormat = self.filmFormat;
metaData.filmQuality = self.filmQuality;
self.userInfos =
@{MovieCreatorAnimations : self.animationLayer,
MovieCreatorAVPlayerItem : playerItem,
MovieCreatorMetadata : metaData};
self.callback(self.userInfos, nil);
}];
}
答案 0 :(得分:3)
我发现了这个问题。 我的AVMutableComposition有多个AVMutableCompositionTrack。如果一个AVMutableCompositionTrack没有曲目,导出的视频将在iOS9上静音但在iOS8上可以正常工作
要解决此问题,当我初始化AVMutableCompositionTrack时,我会添加没有声音的音频。
-(void)initEmptyAudio:(AVMutableCompositionTrack *) obj{
NSString* path = [[NSBundle mainBundle] pathForResource:@"emptyAudio" ofType:@"mp3"];
NSURL* audio_inputFileUrl = [NSURL fileURLWithPath:path];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
[obj insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMake(0,1) error:nil];
}