使用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这绝对适合我!