更新:奇怪的是,这段代码在 iPad 2 上运行良好,但在 iPad 4th Gen 上运行良好。
更新#2:如果我更改presetName:AVAssetExportPresetHighestQuality
为presetName:AVAssetExportPresetPassThrough
视频成功导出但我无法在设备中播放。如果我通过 xCode 的管理器将应用程序包拉到我的计算机上,我就可以播放它。同样,这个问题只发生在 iPad 4 上,而不是 iPad 2、64 位模拟器、视网膜模拟器或 1x 模拟器。
我正在使用AVExportSession
. 它在模拟器和 iPad 2 上运行得非常愉快,但不是 iPad 4th Gen。导出会话给出了一个-11820
错误 ( AVErrorExportFailed
),但这是我可以从该过程中获得的有用信息的范围。源文件存在,其他一切都在正常工作,但不是AVExportSession
.
你能帮我让它在设备上工作吗?
为该方法的冗长道歉。
-(NSURL*)bindAudioAndVideo:(NSString*)audioFileName videoFileName:(NSString*)videoFileName
{
//documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [[NSString alloc] initWithString:[paths objectAtIndex:0]]; //Get the docs directory
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSString* audio_inputFileName = audioFileName;
NSString* audio_inputFilePath = [documentsFolder stringByAppendingPathComponent:audio_inputFileName];
NSURL* audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];
NSString* video_inputFileName = videoFileName;
NSString* video_inputFilePath = [documentsFolder stringByAppendingPathComponent:video_inputFileName];
NSURL* video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];
NSString* outputFileName = @"outputFile.mp4";
NSString* outputFilePath = [documentsFolder stringByAppendingPathComponent:outputFileName];
NSURL* outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
//Check files actually exist before beginning (they do)
AVMutableComposition* mixComposition = [AVMutableComposition composition];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
[self addSkipBackupAttributeToItemAtURL:outputFileUrl];
NSLog(@"Completed. Tidy time.");
switch ([_assetExport status]) {
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export Completed");
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[_assetExport error] localizedDescription]);
NSLog (@"FAIL %@",_assetExport.error); //-11820! I AM A USELESS ERROR CODE
NSLog (@"supportedFileTypes: %@", _assetExport.supportedFileTypes);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export cancelled");
break;
default:
break;
}
NSTimer *refreshTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(exportCompleteRefreshView) userInfo:Nil repeats:NO];
//Throw back to main thread unuless you want really long delays for no reason.
[[NSRunLoop mainRunLoop] addTimer:refreshTimer forMode:NSRunLoopCommonModes];
}
];
return outputFileUrl;
}