使用AVAssetSessionExport不断获取进度信息

时间:2011-10-20 01:29:04

标签: objective-c ios avfoundation

我有一个问题,我正在使用AVAssetSessionExport进行一些视频转换。 我一直在尝试获取UIProgressView的进度信息,但我似乎无法通过这组代码实现这一点。我可以获得有关如何实现这一目标的建议吗?

(看看评论的代码,这就是我如何更新进度条,但它不能正常工作)

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

2 个答案:

答案 0 :(得分:1)

您使用注释代码阻止主线程。您应该使exportSession成为您正在执行此操作的类的属性。然后我建议使用NSTimer定期调用类方法,然后更新progressView直到导出会话完成。我希望这很清楚,如果不是,我可以提供一些代码。

答案 1 :(得分:0)

这就是我在做的事。

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