使用MPMoviePlayerController而不是UIWebView播放YouTubevideo

您好,我试图使用MPMoviePlayerControllerstream一些youTubevideo,但我有一些问题。 我使用的代码非常简单,我可以通过将URL传递给initWithContentURL来播放.m4vvideo。 当我启动电影播放器​​时,播放器出现,但是在大约20秒后才会消失。 当我在模拟器中尝试,我得到一个警告视图,说服务器configuration不正确。 我需要通过url传递一个参数来从Google获取特定types的video订阅源吗?

NSURL *videoURL = [NSURL URLWithString:@"http://www.youtube.com/v/HGd9qAfpZio&hl=en_US&fs=1&"]; MPMoviePlayerController *moviePlayer; moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; [moviePlayer play]; 

我也尝试了以下url的http://www.youtube.com/watch?v=HGd9qAfpZio

我也看到了&format = 1的参数,并试图将这两个string添加到结尾,但没有运气。

提前感谢任何帮助!

在自己的应用程序中进行YouTubevideo播放的唯一方法是使用Youtube中的embedded标记创build一个UIWebView ,以便将其作为UIWebView's内容播放。 UIWebView将检测到embedded对象是Youtube链接,Web视图的内容将成为video的YouTube预览。 当用户点击预览时,video将显示在MPMoviePlayerController中。 这是Muxecoid提供的链接( 如何在应用程序中播放youtubevideo )中描述的技术,这是(据我所知)在应用程序中播放youtubevideo的唯一方法。 您仍然可以通过将youtube URL传递给-[UIApplication openURL:]来启动Youtube应用程序,但是当然这会closures您自己的应用程序,这通常是不可取的。

不幸的是,没有办法直接使用MPMoviePlayerController播放YouTubevideo,因为YouTube不会直接链接到video文件。

如果你使用的是Code:

 - (void)embedYouTube:(NSString*)url 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, url, frame.size.width, frame.size.height]; if(videoView == nil) { videoView = [[UIWebView alloc] initWithFrame:frame]; [self.view addSubview:videoView]; } [videoView loadHTMLString:html baseURL:nil]; } 

资源:

只要参考这个链接

确保你在设备上而不是在模拟器上进行testing。 由于模拟器将始终显示带蓝框的问号(它没有快速播放器)。

正如我在我的评论中写道,我不得不在我正在进行的一个项目中这样做。 我已经解决了这个问题,并在github上提交了代码。

你可以检查源代码并在这里分叉。

它基本上需要一个YouTube的url,并获得所有iOS兼容的videourl。

希望这是对任何帮助!
干杯

我偶然发现了一个能够在MPMoviePlayerController放置YouTubevideo的MPMoviePlayerController 。 现在看起来是可能的。

LBYouTubeView

要播放你pipevideo,你需要提取url之前传递到MPMoviePlayer 。 你不能直接播放。

我的github回购有这个演示实施。

看到:

https://github.com/DpzAtMicRO/IOSYoutubePlayer

试试吧,这样可以直接在MPMoviePlayer加载和播放YouTubevideo,就像其他任何video一样,也是非常好的方法。

编辑:确保你在你的项目中使用这个之前,通过Readme.md

这就像为新的embedded代码调用iframe的src元素一样简单

 [self.webView loadRequest:[NSURLRequest requestWithURL:self.url]]; 

检查以下链接。 我认为这会帮助你。

https://mobiletechfeast.blogspot.in/2014/08/youtube-player-for-ios.html