xcode 在 iPhone 中播放 Youtube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4905701/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:10:43  来源:igfitidea点击:

Play Youtube video in iPhone

iphoneobjective-cxcodeuiwebview

提问by Radix

I want to run a vide from youtube into my iPhone simulator and the URL of the vide is this

我想将来自 youtube 的视频运行到我的 iPhone 模拟器中,视频的 URL 是这样的

http://www.youtube.com/v/zL0CCsdS1cE

http://www.youtube.com/v/zL0CCsdS1cE

but i am not able to load this video into my iPhone, i am using a webview to display this video and here's the code to do that

但我无法将此视频加载到我的 iPhone 中,我正在使用 webview 来显示此视频,这是执行此操作的代码

objwebView = [[UIWebView alloc]initWithFrame:self.view.bounds];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:tempString]];
[objwebView loadRequest:request];

based upon the answer i tried the below code and here's a new version of my code

根据答案,我尝试了以下代码,这是我的代码的新版本

mywebView = [[UIWebView alloc]initWithFrame:self.view.bounds];

NSString *youTubeURL = @"http://www.youtube.com/v/zL0CCsdS1cE";

NSString *htmlString = [NSString stringWithFormat:@"<body style=\"margin:0;background-color:#222222;\"><div style=\"padding:%i;width:%i;height:%i;background-color:#ffffff;\"><video width=\"%i\" height=\"%i\" controls=\"\" autoplay=\"\" tabindex=\"0\"><source src=\"%@\"></source></video></div></body>",10, (int)(self.view.bounds.size.width), (int)(self.view.bounds.size.height), 320, 480,youTubeURL];

[mywebView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:mywebView];

But still i cant get the video to play

但我仍然无法播放视频

plz help

请帮忙

回答by Joseph Tura

I found that it is very convenient to just load an HTML string. Basically like this (replace the variables according to your needs, and fill in the url of the video that youtube loads when displaying video as .mp4):

我发现只加载一个 HTML 字符串非常方便。基本上是这样的(根据您的需要替换变量,并在显示视频为.mp4时填写youtube加载的视频的url):

      NSString *htmlString = [NSString stringWithFormat:
    @"<body style=\"margin:0;background-color:#222222;\">
    <div style=\"padding:%i;width:%i;height:%i;background-color:#ffffff;\">
        <video width=\"%i\" height=\"%i\" controls=\"\" autoplay=\"\" tabindex=\"0\">
    <source src=\"%@\"></source>
        </video>
    </div>
    </body>",
     (int)margin, 
(int)(self.frame.size.width), 
(int)(self.frame.size.height), 
(int)videoWidth, 
(int)videoHeight, 
aURL];
        [objwebView loadHTMLString:htmlString baseURL:nil];

You can find the URL using Charles Debugging Proxy or something similar.

您可以使用 Charles Debugging Proxy 或类似工具找到 URL。

回答by kevboh

According to this reference, just padding the link as an NSURLto [[UIApplication sharedApplication] openURL:myLink]should open the youtube app to the specified video.

根据此参考,只需将链接填充为NSURLto 即可[[UIApplication sharedApplication] openURL:myLink]将 youtube 应用程序打开到指定的视频。

Note that the simulator doesn't have the iPhone app, so this might only work on the device.

请注意,模拟器没有 iPhone 应用程序,因此这可能仅适用于设备。

回答by Bhupendra

hey use iframe and it will play video in simulator and device both..implement it like this:

嘿使用 iframe,它会在模拟器和设备中播放视频..像这样实现它:

NSString *newHTML = @"<html>\
<style>body{padding:0;margin:0;}</style>\
<iframe width=\"640\" height=\"390\" src=\"http://www.youtube.com/embed/zL0CCsdS1cE\" frameborder=\"0\" allowfullscreen></iframe>\
</html>";
[mywebView loadHTMLString:newHTML baseURL:nil];