ios 在 iphone 应用程序中嵌入 youtube 视频

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

embedding youtube video in iphone app

iphoneiosyoutube-api

提问by Abilash

I am new to IOS application development. I've added the code for embedding Youtube video in my app by using UIWebview and the embed code from the youtube. But when I run the application in my simulator, the webview is simply blank. I don't see any video thumbnail or anything else. I heard youtube videos will not run on IPhone simulators but this link ("http://www.youtube.com/embed/36db4r3MsgU")shows that the video is playing perfectly in simulator. kindly look into this link and suggest me a solution.

我是 IOS 应用程序开发的新手。我通过使用 UIWebview 和来自 youtube 的嵌入代码添加了用于在我的应用程序中嵌入 Youtube 视频的代码。但是当我在模拟器中运行应用程序时,webview 只是空白。我没有看到任何视频缩略图或其他任何内容。我听说 YouTube 视频无法在 iPhone 模拟器上运行,但此链接 (" http://www.youtube.com/embed/36db4r3MsgU") 显示该视频在模拟器中完美播放。请查看此链接并建议我一个解决方案。

NSString *code = @"<iframe width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/36db4r3MsgU?feature=player_detailpage\" frameborder=\"0\" allowfullscreen></iframe>";
[[self video]loadHTMLString:code baseURL:nil];

Thanks, Abilash.G

谢谢,Abilash.G

回答by danh

No need to use the embed code. The standard UIWebView aimed at the youtube web address should work fine...

无需使用嵌入代码。针对 youtube 网址的标准 UIWebView 应该可以正常工作...

// URL from safari address bar. 
NSURL *url = [NSURL URLWithString:@"http://www.youtube.com/watch?v=fDXWW5vX-64"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

回答by Nick

@danh answer does work, but it embeds youtube's mobile webpage with the youtube player, which takes a lot of space and it looks terrible. The best option is to follow the instructions in the Youtube Api Blog:

@danh 的回答确实有效,但它将 youtube 的移动网页与 youtube 播放器嵌入在一起,这会占用大量空间,而且看起来很糟糕。最好的选择是按照 Youtube Api 博客中的说明进行操作:

UPDATE: Since youtube now uses v3 of its api, here is a new link with the oficial instructions: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

更新:由于 youtube 现在使用其 api 的 v3,这里是一个带有官方说明的新链接:https: //developers.google.com/youtube/v3/guides/ios_youtube_helper

回答by Ethan Parker

In iOS 8 it started working differently and added padding to the WebView. I posted an answer to this question, this works with iOS 9 and Xcode 7 beautifully. The padding (-8px) may need to be adjusted based on your needs but in general it works amazingly.

在 iOS 8 中,它开始以不同的方式工作并向 WebView 添加填充。我发布了这个问题的答案,这适用于 iOS 9 和 Xcode 7。填充 (-8px) 可能需要根据您的需要进行调整,但总的来说它的效果非常好。

All you need to do it give it the code from any YouTube video (the last characters in any YouTube video URL)

只需为它提供来自任何 YouTube 视频的代码(任何 YouTube 视频 URL 中的最后一个字符)

See post here.

请参阅此处的帖子。

And here is the code:

这是代码:

    CGFloat width = self.webView.frame.size.width;
    CGFloat height = self.webView.frame.size.height;
    NSString *youTubeVideoCode = @"dQw4w9WgXcQ";
    NSString *embedHTML = @"<iframe width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\" style=\"margin:-8px;padding:0;\" allowfullscreen></iframe>";
    NSString *html = [NSString stringWithFormat:embedHTML, width, height, youTubeVideoCode];
    self.webView.scrollView.bounces = NO;
    [self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];

回答by Ali

Google provides a helper to embed and control youtube videos in iOS apps. Have a look at the documentation here: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

Google 提供了一个帮助程序来在 iOS 应用程序中嵌入和控制 youtube 视频。看看这里的文档:https: //developers.google.com/youtube/v3/guides/ios_youtube_helper

回答by Vivek Sehrawat

I have used this in my app. this doesn't use any webview

我在我的应用程序中使用了它。这不使用任何网络视图

https://www.cocoacontrols.com/controls/hcyoutubeparser

https://www.cocoacontrols.com/controls/hcyoutubeparser

OR

或者

NSURL *urlOfYouTube = [NSURL URLWithString:@"http://www.youtube.com..."];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlOfYouTube];
[self.webView loadRequest:requestObj];

回答by Steve B

// MY UIWebview is named "videoYoutube" in this example below // UIWebview size = 320x180 // This below will set the width to the guides ... or close to it and scale dependent on the device size.

// 在下面的示例中,我的 UIWebview 被命名为“videoYoutube” // UIWebview size = 320x180 // 这将设置宽度为指南 ... 或接近它并根据设备大小进行缩放。

    let bounds = UIScreen.mainScreen().bounds
    let width = bounds.size.width - 18
    let height = bounds.size.width / 1.8
    let frame = 0

    let youtubeVideolink = "https://www.youtube.com/embed/Rg6GLVUnnpM"

    let Code:NSString = "<iframe width=\(width) height=\(height) src=\(youtubeVideolink) frameborder=\(frame) allowfullscreen></iframe>"

    self.videoYoutube.loadHTMLString(Code as String, baseURL: nil)?

回答by Quiet Islet

It is too much easy in swift 4

在swift 4中太容易了

let url = URL(string: "http://www.youtube.com/watch?v=fDXWW5vX-64")

let url = URL(string: "http://www.youtube.com/watch?v=fDXWW5vX-64")

let request = URLRequest(url: url!)

let request = URLRequest(url: url!)

webView?.load(request)

webView?.load(request)

回答by R. Mohan

For the first suggested answer i faced some issues like the whole youtube page has been loaded for fixing that i used some extra codes, the codes are

对于第一个建议的答案,我遇到了一些问题,例如整个 youtube 页面都已加载以修复我使用了一些额外的代码,代码是

NSString *baseUrl = [NSString stringWithFormat:@"%@%@",@"http://www.youtube.com/watch?v=",embedCode];
    NSURL *url = [NSURL URLWithString:baseUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    videoWebView = [[UIWebView alloc] initWithFrame:imageview.frame];
    [videoWebView loadRequest:request];
    [videoWebView setNeedsLayout];
    [videoWebView setAllowsInlineMediaPlayback:YES];
    videoWebView.mediaPlaybackRequiresUserAction = YES;
    videoWebView.scrollView.scrollEnabled = NO;
    videoWebView.scrollView.bounces = NO;
    [self.contentView addSubview:videoWebView];