问题描述
我正在实现AVAssetExportSession来在线修剪视频,但总是返回失败.
I'm implementing AVAssetExportSession to trim a video online but always returns failed.
这是我的实现方式
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;
}
}];
你们中的任何人都知道为什么会这样或者我在做什么错吗?
Any of you knows why this happening or what I'm doing wrong?
推荐答案
您正在尝试使用远程URL创建AVAsset
,并且需要知道资产已加载,然后才能开始导出.
You are attempting to create an AVAsset
with a remote URL and you need to know that the asset has loaded before you can begin your export.
AVAsset
符合AVAsynchronousKeyValueLoading
协议,这意味着您可以观察tracks
键并在值更改后开始导出:
AVAsset
conforms to the AVAsynchronousKeyValueLoading
protocol, which means you can observe the tracks
key and start your export once the value changes:
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];
}];
然后,您可以使用单独的方法来导出代码:
Then you can have your export code in a separate method:
- (void)exportAsset:(AVAsset *)asset {
//Your export code here
}
这篇关于AVAssetExportSession exportAsynchronouslyWithCompletionHandler返回失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!