Adrian
Adrian

Reputation: 8211

getting progress information constantly with AVAssetSessionExport

I have an issue where I'm using AVAssetSessionExport to do some video conversions. I've been trying to get the progress information on a UIProgressView but i can't seem to achieve that with this set of code. Could I get suggestions on how I might be able to achieve this?

(look at the commented code, that's how i'm updating the progress bar, but it doesn't work well)

   NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:composition];
    if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                               initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:indexPath];

//        UIProgressView *prog = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
//        [cell.contentView addSubview:prog];
//        
        exportSession.outputURL = [NSURL fileURLWithPath:[[ShowDAO userDocumentDirectory] stringByAppendingString:exportFilename]];
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;

        CMTime start = CMTimeMakeWithSeconds(0, 1);
        CMTime duration = CMTimeMakeWithSeconds(1000, 1);
        CMTimeRange range = CMTimeRangeMake(start, duration);
        exportSession.timeRange = range;
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            switch ([exportSession status]) {
                case AVAssetExportSessionStatusCompleted:
                    NSLog(@"Export Completed");
                    [DSBezelActivityView removeViewAnimated:YES];
                    //delete unused video file
                    [[NSFileManager defaultManager] removeItemAtPath: [[ShowDAO userDocumentDirectory] stringByAppendingString:videoFilename] error: NULL];
                    break;
                case AVAssetExportSessionStatusFailed:
                    NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
                    break;
                case AVAssetExportSessionStatusCancelled:
                    NSLog(@"Export cancelled");
                    break;
                default:
                    break;
            }


        }];
//        while (exportSession.progress <=0.1){
//            NSLog(@"prog : %f",exportSession.progress);
//            [prog setProgress:exportSession.progress];
////            sleep(1);
//        }
        [exportSession release];

Upvotes: 0

Views: 2410

Answers (2)

Henry
Henry

Reputation: 479

This is what i am doing.

    BOOL goOnFlag = YES;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        int count = 0;
        while(fabs(avAssetExportSession.progress - 1.0)>0.01&&goOnFlag){
            NSLog(@"loading... : %f",avAssetExportSession.progress);
            sleep(1);
            if(avAssetExportSession.progress==0)
                count++;
            if(count>8){
                [self async_main:^{
                    [avAssetExportSession cancelExport];
                    goOnFlag = NO;
                    NSLog(@"save failed");
                }];
            }
        }
    });

Upvotes: 0

huesforalice
huesforalice

Reputation: 2458

You're blocking the main thread with the commented code. You should make the exportSession a property of the class you're doing this in. Then I'd suggest having an NSTimer which calls a class method periodically which then updates the progressView until the export session is done. I hope this is clear, if not I can provide some code.

Upvotes: 1

Related Questions