我正在创建一个视频应用,我使用AVAssetExportSession创建一个新视频。在创建视频时,我想让用户能够取消视频创建。我遇到的问题是我不知道如何向AVAssetExportSession发送取消请求,因为我认为它正在主线程上运行。一旦启动,我不知道如何发送停止请求?
我尝试了这个,但它不起作用
- (IBAction) startBtn
{
....
// Export
exportSession = [[AVAssetExportSession alloc] initWithAsset:[composition copy] presetName:AVAssetExportPresetHighestQuality];
[exportSession setOutputFileType:@"com.apple.quicktime-movie"];
exportSession.outputURL = outputMovieURL;
exportSession.videoComposition = mainComposition;
//NSLog(@"Went Here 7 ...");
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status])
{
case AVAssetExportSessionStatusCancelled:
NSLog(@"Canceled ...");
break;
case AVAssetExportSessionStatusCompleted:
{
NSLog(@"Complete ... %@",outputURL); // moview url
break;
}
case AVAssetExportSessionStatusFailed:
{
NSLog(@"Faild=%@ ...",exportSession.error);
break;
}
case AVAssetExportSessionStatusExporting:
NSLog(@"Exporting.....");
break;
}
}];
}
- (IBAction) cancelBtn
{
exportSession = nil;
}
答案 0 :(得分:4)
您可以通过向其发送消息cancelExport
来取消导出会话。
要做到这一点,你只需要一个包含当前活动导出会话的ivar(或属性):
@property (nonatomic, strong) AVAssetExportSession* exportSession;
初始化属性:
- (IBAction) startBtn {
if (self.exportSession == nil) {
self.exportSession = [[AVAssetExportSession alloc] initWithAsset:[composition copy]
presetName:AVAssetExportPresetHighestQuality];
...
[self.exportSession exportAsynchronouslyWithCompletionHandler:^{
self.exportSession = nil;
....
}];
}
else {
// there is an export session already
}
}
为了取消会话:
- (IBAction) cancelBtn
{
[self.exportSession cancelExport];
self.exportSession = nil;
}
提示:为了获得更好的用户体验,您应该相应地禁用/启用“取消”和“开始导出”按钮。