在您的 iOS 设备上无需痛苦地本地播放 YouTube、Vimeo 以及 .MP4、.MOV、.MPV、.3GP 视频并获取缩略图。
如果您一直在 UIWebView
中播放 YouTube 和 Vimeo 视频是因为难以找到可以直接通过 MPMoviePlayerViewController
在 iOS 设备上本地播放的 .MP4 的直接 URL 以获得更优质的用户体验,那么 YKMediaPlayerKit
就是为了您。
它不仅帮助您通过单个方法调用播放在线 YouTube、Vimeo 以及所有原生支持的格式(如 .MP4、.MOV、.MPV、.3GP),还允许您异步下载视频的缩略图。您还可以选择低、中、高分辨率的视频或缩略图。
它是基于块的,异步工作,并在主线程上返回所有块回调。
//Variable to hold parsed video
id<YKVideo> video;
//You can use YouTube, Vimeo, or direct video URL too.
NSString *videoURL = @"https://www.youtube.com/watch?v=GJey_oygU3Y";
YKMediaPlayerKit *player = [[YKMediaPlayerKit alloc] initWithURL:[NSURL URLWithString:videoURL]];
[player parseWithCompletion:^(YKVideoTypeOptions videoType, id<YKVideo> parsedVideo, NSError *error) {
//Save parsed video to a class level property.
video = parsedVideo;
//Get thumbnails
[player thumbWithCompletion:^(UIImage *thumb, NSError *error) {
if (thumb) {
//set thumbnail to respective UIImageView.
self.imageView.image = thumb;
}
}];
}];
//Play parsed video
- (IBAction)playButtonPressed {
[self.video play:YKQualityMedium];
}
或者,您也可以这样做:
播放 YouTube 视频,并获取其缩略图。
NSString *videoLink = @""http://www.youtube.com/watch?v=1hZ98an9wjo";"
YKYouTubeVideo *youTubeVideo = [[YKYouTubeVideo alloc] initWithContent:[NSURL URLWithString:videoLink]];
//Fetch thumbnail
[youTubeVideo parseWithCompletion:^(NSError *error) {
[youTubeVideo thumbImage:YKQualityLow completion:^(UIImage *thumbImage, NSError *error) {
//Set thumbImage to UIImageView here
}];
}];
//Then play (make sure that you have called parseWithCompletion before calling this method)
[youTubeVideo play:YKQualityHigh];
对于 Vimeo 视频也是如此。
NSString *videoLink = @"http://vimeo.com/42893621";
YKVimeoVideo *vimeoVideo = [[YKVimeoVideo alloc] initWithContent:[NSURL URLWithString:videoLink]];
[vimeoVideo parseWithCompletion:^(NSError *error) {
[vimeoVideo thumbImage:YKQualityLow completion:^(UIImage *thumbImage, NSError *error) {
//Set thumbImage to UIImageView here
}];
}];
//Then play (make sure that you have called parseWithCompletion before calling this method)
[vimeoVideo play:YKQualityHigh];
最后播放并从 iOS 支持的原生视频格式(如 .MP4、.MOV、.MPV、.3GP)获取缩略图
NSString *videoLink = @"http://download.wavetlan.com/SVV/Media/HTTP/BlackBerry.mov";
YKDirectVideo *directVideo = [[YKDirectVideo alloc] initWithContent:[NSURL URLWithString:videoLink]];
[directVideo thumbImage:YKQualityLow completion:^(UIImage *thumbImage, NSError *error) {
//Set thumbImage to UIImageView here
}];
//Then play
[directVideo play:YKQualityHigh];
如果您使用 CocoaPods
,请在您的 pods 文件中添加以下内容并更新 pods pod 'YKMediaPlayerKit'
或者,简单地将 YKMediaPlayerKit
文件夹拖放到您的项目中。
The MIT License (MIT)
版权(c)2014 Yas Kuraishi
在此特此授予任何获得此软件和关联文档副本(“软件”)的人免费的许可,不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,以及允许向软件提供的人做上述事项,前提是
所有副本或大部分软件中均应包含上述版权声明和本许可声明。
软件按“原样”提供,不提供任何形式的保证,无论是明示还是隐含的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,源于、涉及或与软件及其使用或任何其他处置相关。
HCYoutubeParser 由 Simon Andersson 开发 - https://github.com/hellozimi/HCYoutubeParser