AVAssetExportSession exportAsynchronouslyWithCompletionHandler返回失败

我正在实施AVAssetExportSession来在线修剪video,但总是返回失败。

这是我的实施:

NSString *url = @"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"; NSURL *fileURL = [NSURL URLWithString:url]; AVAsset *asset = [AVAsset assetWithURL:fileURL]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality]; NSURL *exportUrl = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"export.m4a"]]; exportSession.outputURL = exportUrl; exportSession.outputFileType = AVFileTypeQuickTimeMovie; CMTime time = CMTimeMake(1, 10); exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, time); [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { switch (exportSession.status) { case AVAssetExportSessionStatusCompleted: /*expor is completed*/ NSLog(@"Completed!!"); break; case AVAssetExportSessionStatusFailed: NSLog(@"failed!!"); /*failed*/ break; default: break; } }]; 

你们中的任何人都知道为什么会这样或者我做错了什么?

您正在尝试使用远程URL创建AVAsset ,并且在开始导出之前需要知道资产已加载。

AVAsset符合AVAsynchronousKeyValueLoading协议,这意味着您可以观察tracks键并在值更改后开始导出:

 NSURL *myURL = [NSURL URLWithString:myMovieURLString]; AVAsset *asset = [AVAsset assetWithURL:myURL]; __weak typeof(self) weakSelf = self; [asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{ //Error checking here - make sure there are tracks [weakSelf exportAsset:asset]; }]; 

然后,您可以使用单独的方法获取导出代码:

 - (void)exportAsset:(AVAsset *)asset { //Your export code here } 

documentsDirectory应该是一个正在退出的路径。 如果没有,exportSession.status将等于AVAssetExportSessionStatusFailed