ios 在不使用 UIWebView 的情况下在 iPhone 应用程序中播放 YouTube 视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9060153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play YouTube videos in iPhone app without using UIWebView?
提问by Yuvaraj.M
I want to play YouTube videos from my iPhone app. I have to tried play YouTube videos in my iPhone app with the following codes,
我想从我的 iPhone 应用程序播放 YouTube 视频。我必须尝试使用以下代码在我的 iPhone 应用程序中播放 YouTube 视频,
[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);
}
But, this code is not working for me. And also i have tried with MPMoviePlayerController
the code is,
但是,这段代码对我不起作用。而且我也尝试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];
This is also not working for me. Can anyone please tell me where i did wrong and suggest me any ideas? I want to play the YouTube videos in MPMoviewPlayerController
rather than using UIWebView
. Thanks in advance.
这也不适合我。谁能告诉我我做错了什么并建议我任何想法?我想播放 YouTube 视频MPMoviewPlayerController
而不是使用UIWebView
. 提前致谢。
采纳答案by Gopinath
Mr.Yuvaraj.M your above code is correct. Please run the project in your iPhone/iPod touch device. The youtube videos may not support/not work in Simulator. Please check on this. Thanks.
Mr.Yuvaraj.M 你上面的代码是正确的。请在您的 iPhone/iPod touch 设备上运行该项目。youtube 视频可能不支持/无法在模拟器中运行。请检查一下。谢谢。
回答by Hlung
Just use XCDYouTubeVideoPlayerViewController. It seamlessly adds youtube video to your app without the need of UIWebView
.
只需使用XCDYouTubeVideoPlayerViewController。它可以无缝地将 youtube 视频添加到您的应用程序中,而无需UIWebView
.
It uses progressive download + MoviePlayer
and AVFoundation
frameworks. Just supply it with a youtube video ID and you are good to go.
它使用渐进式下载+MoviePlayer
和AVFoundation
框架。只需为其提供一个 YouTube 视频 ID,您就可以开始使用了。
It supports both full-screen & non full-screen and WORKS ON iOS SIMULATOR!!!
它支持全屏和非全屏以及适用于 iOS 模拟器!!!
Example:
例子:
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
回答by Max MacLeod
There is now a YTPlayerView
class available with the YouTube Data API (v3).
YouTube Data API (v3)现在YTPlayerView
提供了一个类。
Have a look at Using the YouTube Helper Library to embed YouTube videos in your iOS application
查看使用 YouTube 帮助程序库将 YouTube 视频嵌入您的 iOS 应用程序
It gives comprehensive instructions on how to install the helper library, and incorporate YTPlayerView into your storyboard.
它提供了有关如何安装帮助程序库以及将 YTPlayerView 合并到故事板中的全面说明。
回答by Rob Reuss
Instead of the HTML embed code you are using, go to the YouTube website, find a video, use the share button and then the Embed button to get some example Embed HTML. Use that pattern for embedding the video rather than the Flash version you are currently using.
不要使用您正在使用的 HTML 嵌入代码,而是转到 YouTube 网站,找到一个视频,使用共享按钮,然后使用嵌入按钮来获取一些嵌入 HTML 的示例。使用该模式嵌入视频,而不是您当前使用的 Flash 版本。
回答by Mahesh Chaudhari
Play youtube video in swift 4.1
在 swift 4.1 中播放 youtube 视频
Add Pod pod 'XCDYouTubeKit'
添加 Pod pod 'XCDYouTubeKit'
import XCDYouTubeKit in ViewController
在 ViewController 中导入 XCDYouTubeKit
func playYoutubeVideo(){
let strUrl = "https://www.youtube.com/watch?v=9m_K2Yg7wGQ"
if let range = strUrl.range(of: "=") {
let strIdentifier = strUrl.substring(from: range.upperBound)
print("Identifier:\(strIdentifier)")
let videoPlayerViewController =
XCDYouTubeVideoPlayerViewController(videoIdentifier: strIdentifier)
videoPlayerViewController.present(in: viewYTVideo)
videoPlayerViewController.moviePlayer.play()
}
}