AVAssetExportSession exportAsynchronouslyWithCompletionHandler返回失败

时间:2015-10-04 06:59:32

标签: ios objective-c avfoundation avassetexportsession avkit

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

这是我的实施:

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;
    }
}];

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

2 个答案:

答案 0 :(得分:1)

您正在尝试使用远程网址创建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
}

答案 1 :(得分:0)

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