如何显示youtube视频文件的下载进度

时间:2014-07-09 08:13:54

标签: ios video youtube download progress

在我的iOS应用程序中,我使用以下代码将YouTube视频文件下载到iPhone

LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(@"http://www.youtube.com/watch?v=%@"), videoURL ]] quality:LBYouTubeVideoQualityLarge] autorelease];

   [extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURLL, NSError *error) {
        if(!error) {   

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSData *videoData = [NSData dataWithContentsOfURL:videoURLL ];
                NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                NSString *filename = [NSString stringWithFormat:(@"video_%@.mp4"),videoURL];

                [videoData writeToFile:[pathToDocs stringByAppendingPathComponent:filename ] atomically:YES];

            });


 } else {
            NSLog(@"Failed extracting video URL using block due to error:%@", error);
        }
    }];

我想在我的应用中显示下载进度。我该如何计算文件大小和下载的百分比。

1 个答案:

答案 0 :(得分:0)

将bytesDownloaded和downloadSize声明为float。如果它们是int,则需要将它们转换为浮点值。

long double precent = (bytesDownloaded/downloadSize) * 100.0;