使用MPMoviePlayerController播放youtubevideo
我一直在使用一个HTMLstring很长一段时间通过UIWebView播放YouTubevideo,问题是我想要播放状态更改通知。 我决定创build一个MPMoviePlayerController并通过这个播放YouTubevideo,但似乎无法使它工作。 我在viewdidload中使用以下代码:
NSString *urlAddress = @"http://www.youtube.com/watch?v=m01MYOpbdIk"; NSURL *url = [NSURL URLWithString:urlAddress]; CGFloat width = [UIScreen mainScreen].bounds.size.width; CGFloat height = [UIScreen mainScreen].bounds.size.height; movie = [[MPMoviePlayerController alloc] initWithContentURL:url]; movie.scalingMode=MPMovieScalingModeAspectFill; movie.view.frame = CGRectMake(0.0, 0.0, width, height); [self.view addSubview:movie.view]; [movie play];
给我这个错误:
_itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }
利用自定义的LBYouTubePlayerViewController
它是MPMoviePlayerViewController
一个子类。
LBYouTubeView只是一个小视图,可以在MPMoviePlayerController
显示YouTubevideo。 您甚至可以在高质量和标准质量stream之间进行select。
它只是加载YouTube手机网站的HTML代码,并在脚本标签中查找数据。
LBYouTubeView 不使用UIWebView ,使其更快,看起来更干净。
对我来说,这个图书馆的工作完美! https://github.com/hellozimi/HCYoutubeParser
moviePlayer = [[MPMoviePlayerController alloc] init]; moviePlayer.shouldAutoplay = YES; moviePlayer.fullscreen = YES; moviePlayer.repeatMode = MPMovieRepeatModeNone; moviePlayer.controlStyle = MPMovieControlStyleDefault; moviePlayer.movieSourceType = MPMovieSourceTypeFile; moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
在这个调用方法之后。
[self callYouTubeURL:[NSString stringWithFormat:@"http://www.youtube.com/embed/%@",_urlcode]];
在这个方法parsingyoutube链接。
- (void)callYouTubeURL:(NSString *)urlLink { NSURL *url = [NSURL URLWithString:urlLink]; actvity.hidden = NO; [HCYoutubeParser thumbnailForYoutubeURL:url thumbnailSize:YouTubeThumbnailDefaultHighQuality completeBlock:^(UIImage *image, NSError *error) { if (!error) { [HCYoutubeParser h264videosWithYoutubeURL:url completeBlock:^(NSDictionary *videoDictionary, NSError *error) { NSDictionary *qualities = videoDictionary; NSString *URLString = nil; if ([qualities objectForKey:@"small"] != nil) { URLString = [qualities objectForKey:@"small"]; } else if ([qualities objectForKey:@"live"] != nil) { URLString = [qualities objectForKey:@"live"]; } else { [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Couldn't find youtube video" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show]; return; } _urlToLoad = [NSURL URLWithString:URLString]; [self urlLoadintoPlayer]; }]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [alert show]; } }]; }
在这个负载之后新parsingurl成为播放器。
-(void)urlLoadintoPlayer { moviePlayer.contentURL = _urlToLoad; }
官方的方式来播放YouTubevideo:1)UIWebView与UIWebView的内容中的embedded标签。2)使用youtube-ios-player-helper [谷歌官方的方式]
不幸的是,没有办法直接使用MPMoviePlayerController播放YouTubevideo,因为YouTube不会直接链接到video文件。
有通过MPMoviePlayerController运行youtubevideo的库,但是它们反对YouTube的TOC。 因此最简单的方法是去youtube-ios-player-helper。
如果youtube-ios-player-helper pod不起作用,您可以将YTPlayer.h / .m和assets文件夹添加到您的项目中,并使用#import“YTPlayerView.h”编写一个网桥头文件,您可以在https ://github.com/youtube/youtube-ios-player-helper这绝对适合我!
- 捆绑ID是否需要区分大小写?
- 在iOS中定制形状(倒T)镶嵌Uiview
- 当应用程序处于后台时,使用AmbientSound会话的AVAudioPlayer不会播放
- 我们怎样才能隐藏tableHeaderView和tableFooterView?
- 如何解决auth错误“redirectURI不匹配注册的redirectURI”在IOS?
- _OBJC_CLASS _ $ _ SBJsonParser引用来自:错误
- 点击已经select的段控制swift
- 在iOS中保持应用程序空闲一段时间后,SRWebsocket连接将自动closures
- 如何使用AFNetwork的AFHTTPRequestOperationManager设置HTTP请求正文?