在iPhone应用程序中播放YouTubevideo而不使用UIWebView?

我想从我的iPhone应用程序播放YouTubevideo。 我必须尝试使用​​以下代码在iPhone应用程序中播放YouTubevideo,

[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)]; - (void)playVideo:(NSString *)urlString frame:(CGRect)frame { NSString *embedHTML = @"\ <html><head>\ <style type=\"text/css\">\ body {\ background-color: transparent;\ color: white;\ }\ </style>\ </head><body style=\"margin:0\">\ <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ width=\"%0.0f\" height=\"%0.0f\"></embed>\ </body></html>"; NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height]; UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame]; [videoView loadHTMLString:html baseURL:nil]; [self.view addSubview:videoView]; [videoView release]; NSLog(@"%@",html); } 

但是,这个代码不适合我。 而且我也试着用MPMoviePlayerController的代码是,

 NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"]; MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; [self.view addSubview:moviePlayerController.view]; moviePlayerController.fullscreen = YES; [moviePlayerController play]; 

这也不适合我。 任何人都可以告诉我,我做错了,并build议我有什么想法? 我想在MPMoviewPlayerController播放YouTubevideo,而不是使用UIWebView 。 提前致谢。

Yuvaraj.M先生你的上面的代码是正确的。 请在您的iPhone / iPod touch设备上运行该项目。 YouTubevideo可能不支持/不能在模拟器中工作。 请检查这一点。 谢谢。

只需使用XCDYouTubeVideoPlayerViewController 。 它无缝地将youtubevideo添加到您的应用程序,而不需要UIWebView

它使用渐进式下载+ MoviePlayerAVFoundation框架。 只要提供一个YouTubevideoID,你很好去。

它支持全屏幕和非全屏幕和工作在iOS模拟器!

例:

 XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"]; [self presentMoviePlayerViewControllerAnimated:videoPlayerViewController]; 

YouTube数据API(v3)现在有一个YTPlayerView类。

查看使用YouTube帮助程序库在您的iOS应用程序中embeddedYouTubevideo

它提供了有关如何安装帮助程序库的详细说明,并将YTPlayerView合并到故事板中。

而不是您正在使用的HTMLembedded代码,转到YouTube网站,find一个video,使用共享button,然后使用embeddedbutton来获得一些embeddedHTML的例子。 使用该模式embeddedvideo,而不是您当前使用的Flash版本。