在我的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);
}
}];
我想在我的应用中显示下载进度。我该如何计算文件大小和下载的百分比。
答案 0 :(得分:0)
将bytesDownloaded和downloadSize声明为float。如果它们是int,则需要将它们转换为浮点值。
long double precent = (bytesDownloaded/downloadSize) * 100.0;