在应用中播放Youtube视频非常简单,并且有很好的文档记录。
有两个问题:
有没有人有代码:
答案 0 :(得分:42)
从YouTube下载视频:
NSTemporaryDirectory()
或文档目录中的临时命名文件)中创建NSOutputStream或NSFileHandle。sendSynchronousRequest:returningResponse:error:
。connection:didReceiveResponse:
委托方法中,读出要下载的数据长度,以便正确更新进度条。connection:didReceiveData:
委托方法中,将数据写入输出流/文件句柄并根据需要更新进度条。connectionDidFinishLoading:
或connection:didFailWithError:
中,关闭输出流/文件句柄,并根据需要重命名或删除临时文件。要播放它,只需使用NSURL的fileURLWithPath:
创建一个指向Documents目录中本地文件的URL,并像播放任何远程视频一样播放它。
答案 1 :(得分:7)
我使用过这个项目中的类:https://github.com/larcus94/LBYouTubeView 这对我来说可以。 我可以下载youtube视频。
我使用了这段代码:
LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(@"http://www.youtube.com/watch?v=%@"), self.videoID ]] quality:LBYouTubeVideoQualityLarge] autorelease];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
if(!error) {
NSLog(@"Did extract video URL using completion block: %@", videoURL);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL: videoURL];
NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [NSString stringWithFormat:(@"video_%@.mp4"), self.videoID ];
[data writeToFile:[pathTODocs stringByAppendingPathComponent:filename] atomically:YES];
NSLog(@"File %@ successfully saved", filename);
});
} else {
NSLog(@"Failed extracting video URL using block due to error:%@", error);
}
}];
您可以使用上述帖子中描述的技术显示下载进度。
答案 2 :(得分:6)
以下是我的示例:https://github.com/comonitos/youtube_video
我使用了Peter Steinberger的PSYouTubeExtractor.h课程它可以获得youtube mp4视频网址,而不是下载和查看不是问题
NSURLConnection的 + NSNotificationCenter + PSYouTubeExtractor + NSMutableData
答案 3 :(得分:5)
我个人不知道如何下载youtube视频(而且代码太大,无法在此处提出答案)。
但是,这是一个完整的YouTube下载示例here。 它是一个名为LoadTube的开源youtube下载程序:here's the a link to the source code。
答案 4 :(得分:5)
答案 5 :(得分:1)
我会播放视频并找出临时文件的存储位置。如果您可以访问它,请将其复制到某个文档文件夹中以供离线查看。