Bik*_*iko 27 youtube iphone offline-mode objective-c
在应用程序中播放Youtube视频很容易,并且有很好的文档记录.
有两个问题:
有没有人有代码:
Ano*_*mie 42
要从YouTube下载视频:
NSTemporaryDirectory()
.sendSynchronousRequest:returningResponse:error:
当然不要使用.connection:didReceiveResponse:
委托方法中,读出要下载的数据长度,以便正确更新进度条.connection:didReceiveData:
委托方法中,将数据写入输出流/文件句柄并根据需要更新进度条.connectionDidFinishLoading:
或中connection:didFailWithError:
,关闭输出流/文件句柄,并根据需要重命名或删除临时文件.要播放它,只需使用NSURL fileURLWithPath:
创建一个指向Documents目录中本地文件的URL,并像播放任何远程视频一样播放它.
我使用过这个项目的类: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);
}
}];
Run Code Online (Sandbox Code Playgroud)
您可以使用上面帖子中描述的技术显示下载进度.
这是我的例子:https://github.com/comonitos/youtube_video
我使用了Peter Steinberger的PSYouTubeExtractor.h课程它可以获得youtube mp4视频网址而不是下载和查看不是问题
NSURLConnection + NSNotificationCenter + PSYouTubeExtractor + NSMutableData