在dispatch_async中从AVURLAsset获取视频持续时间时,UI被阻止

时间:2016-09-21 13:02:10

标签: ios objective-c avplayer dispatch-async avurlasset

我有2个View Controllers Home和Home Details。在Home我有一个表格视图,其中我显示了视频的缩略图和持续时间。当我点击特定行时,它的详细信息显示在“主页详细信息”中。返回时我正在更新所选行。因此,在viewWillDisappear Home of Home Details方法中,我编写了以下代码:

if ([self.delegate respondsToSelector:@selector(changeSelectedBucketData:)]) {
        [self.delegate changeSelectedBucketData:_videoId];
    } 

现在在Home Controller中我将该方法定义为:

-(void)changeSelectedBucketData:(NSString*)videoId {

    NSString *dataStr = [NSString stringWithFormat:@"%@bucket_id=%@",kGetBucketById,videoId];

    [[WebServiceCall sharedInstance] sendGetRequestToWebWithData:dataStr success:^(NSDictionary *json) {

        if([[json valueForKey:@"ResponseCode"] integerValue] == 0) {

        } else {
            dispatch_async(dispatch_get_main_queue(), ^{

                [_arrayOfContent replaceObjectAtIndex:selectedIndex withObject:[json valueForKey:@"GetData"]];

                if (_arrayOfContent.count) {

                    TableViewCellHome *cell = [self.mTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0]];

                    [self fillDataForIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0] forCell:cell];
                }
            });
        }
    } failure:^(NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    }];
}



-(void)fillDataForIndexPath:(NSIndexPath*)indexPath forCell:(TableViewCellHome*)cell{

    NSDictionary *dict = [_arrayOfContent objectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:[[_arrayOfContent objectAtIndex:indexPath.row] valueForKey:@"video_URL"]];
            [self downloadDurationAtURL:url cellTag:indexPath];
}

现在我使用以下代码下载视频的持续时间:

- (NSUInteger)videoDuration:(NSURL *)videoURL {

    AVURLAsset *videoAVURLAsset = [AVURLAsset assetWithURL:videoURL];

    CMTime durationV = videoAVURLAsset.duration;

    return CMTimeGetSeconds(durationV);
}

- (NSString *)videoDurationTextDurationTotalSeconds:(NSUInteger)dTotalSeconds {

    NSUInteger dHours = floor(dTotalSeconds / 3600);
    NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
    NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);

    if (dHours > 0) {

        return [NSString stringWithFormat:@"%i:%02i:%02i",dHours, dMinutes, dSeconds];

    } else {

        return [NSString stringWithFormat:@"%02i:%02i",dMinutes, dSeconds];
    }
}


-(void)downloadDurationAtURL:(NSURL *)videoURL cellTag:(NSIndexPath*)indexPath {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         //retrive image on global queue

        NSUInteger dTotalSeconds = [self videoDuration:videoURL];

        NSLog(@"dTotalSeconds %i",dTotalSeconds);

        if (dTotalSeconds > 0) {

            NSString *videoDurationText = [self videoDurationTextDurationTotalSeconds:dTotalSeconds];

            dispatch_async(dispatch_get_main_queue(), ^{

                 TableViewCellHome *cell = [self.mTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
                [[_arrayOfContent objectAtIndex:indexPath.row] setObject : videoDurationText  forKey:@"duration"];
                cell.labelDuration.text = videoDurationText;
                cell.labelDuration.hidden = false;
            });
        }
        else {

        dispatch_async(dispatch_get_main_queue(), ^{
             TableViewCellHome *cell = [self.mTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
                 [[_arrayOfContent objectAtIndex:indexPath.row] setObject : @"" forKey:@"duration"];
                cell.labelDuration.hidden = true;
                cell.labelDuration.text = @"";
            });
        }
    });
}

现在问题是UI被阻止,直到单元格中的持续时间发生变化。在单元格上显示持续时间之前,我无法选择特定行。但是在调用API后第一次显示Home控制器时它工作正常。当我从Home detail中调用它时,它才会被阻止。

1 个答案:

答案 0 :(得分:0)

您需要异步加载持续时间,如下所示:

- (void)videoDuration:(NSURL *)videoURL completion:(void (^)(CMTime))durationCallback {
    AVURLAsset *videoAVURLAsset = [AVURLAsset assetWithURL:videoURL];

    [videoAVURLAsset loadValuesAsynchronouslyForKeys:@[ @"duration"] completionHandler:^{
        NSError *error;
        if([videoAVURLAsset statusOfValueForKey:@"duration" error:&error]) {
            NSLog(@"error getting duration: %@", error);
            durationCallback(kCMTimeZero);  // or something
        } else {
            durationCallback(videoAVURLAsset.duration);
        }
    }];
}