使用 MPMoviePlayerController 而不是 UIWebView 播放 YouTube 视频 [英] Play YouTube videos with MPMoviePlayerController instead of UIWebView

查看:29
本文介绍了使用 MPMoviePlayerController 而不是 UIWebView 播放 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MPMoviePlayerController 流式传输一些 YouTube 视频,但我遇到了一些问题.我使用的代码非常简单,我可以通过将 URL 传递给 initWithContentURL 来播放 .m4v 视频.当我启动电影播放器​​时,播放器出现但在大约 20 秒后消失.当我在模拟器中尝试时,我收到一个警告视图,指出服务器配置不正确.是否需要通过 URL 传递参数才能从谷歌获取特定类型的视频源?

I'm trying to stream some youTube videos using the MPMoviePlayerController but I'm having some problems. The code I'm using is pretty simple and I can play .m4v videos by passing a URL to initWithContentURL. When I launch the movie player the player comes up but just goes away after about 20 seconds. When I try it in the simulator I get an alert view that says the server is not configured correctly. Is there an argument I need to pass with the URL to get a specific type of video feed from google?

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

[moviePlayer play];

我也试过以下网址http://www.youtube.com/watch?v=HGd9qAfpZio

我也看到了参数 &format=1 并试图将它添加到两个字符串的末尾,但没有成功.

I have also seen the argument &format=1 and tried to add that to the end of both of the strings but no luck.

推荐答案

在您自己的应用程序中播放 youtube 视频的唯一方法是创建一个 UIWebView 和来自 Youtube 的 embed 标签,用于你想作为 UIWebView's 内容播放的电影.UIWebView 会检测到嵌入的对象是一个 Youtube 链接,而 web 视图的内容将是视频的 youtube 预览.当您的用户单击预览时,视频将显示在 MPMoviePlayerController 中.这是 Muxecoid 提供的链接中描述的技术 (如何在应用程序中播放 youtube 视频),这是(据我所知)在应用程序中播放 youtube 视频的唯一方法.您仍然可以通过将 youtube URL 传递给 -[UIApplication openURL:] 来启动 Youtube 应用程序,但这当然会关闭您自己的应用程序,这通常是不受欢迎的.

The only way to have a youtube video play inside your own app is to create a UIWebView with the embed tag from Youtube for the movie you want to play as the UIWebView's content. UIWebView will detect that the embedded object is a Youtube link, and the web view's content will be the youtube preview for the video. When your user clicks the preview, the video will be shown in an MPMoviePlayerController. This is the technique described at the link that Muxecoid provided (how to play youtube videos within an application), and this is (as far as I know of) the only way to have a youtube video play within an application. You can still have the Youtube application launch by passing the youtube URL to -[UIApplication openURL:], but of course this closes your own application which is often undesirable.

遗憾的是,无法使用 MPMoviePlayerController 直接播放 youtube 视频,因为 youtube 不公开视频文件的直接链接.

Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerController because youtube does not expose direct links to the video files.

这篇关于使用 MPMoviePlayerController 而不是 UIWebView 播放 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆