简体   繁体   中英

AVAssetExportSession send cancel export

I am making a video app where I create a new video using AVAssetExportSession. While the video is being created I want to give the user ability to cancel video creation. The problem I have is that I do not know how can I send a cancellation request to AVAssetExportSession as I assume it's running on the main thread. Once it starts I have no idea how can I send a stop request?

I tried this but it doesn't work

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

You can cancel an export session by sending it the message cancelExport .

To accomplish this, you simply need to have an ivar (or property) which holds the current active export session:

@property (nonatomic, strong) AVAssetExportSession* exportSession;

Initialize the property:

- (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
    }
}

In order to cancel the session:

- (IBAction) cancelBtn
{
    [self.exportSession cancelExport];
    self.exportSession = nil;
}

Hint: For a better user experience, you should disable/enable "Cancel" and "Start Export" buttons accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM