1

我正在制作一个视频应用程序,在其中使用 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;
}
4

1 回答 1

5

您可以通过向其发送消息来取消导出会话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;
}

提示:为了获得更好的用户体验,您应该相应地禁用/启用“取消”和“开始导出”按钮。

于 2013-11-17T13:57:59.477 回答