在iframe中使用YouTube嵌入播放器在iOS6中不起作用 [英] YouTube Embed player in Iframe doesn't work in iOS6
问题描述
我真的需要你的帮助。在iOS应用上工作。我想玩youtube。我从阅读许多博客和帖子中了解到,我们需要使用iframe来播放YouTube视频。
I really need your help. Working on an iOS app. I want to play youtube. I understood from reading many many blogs and posts, that we need to use an iframe in order to play a youtube video.
然而,在某些视频中,我得到:此视频包含来自XYZ的内容。限制在某些网站上播放。在YouTube上观看
However, on some videos I get: "This video contains content from XYZ. It is restricted from playback on certain sites. Watch on YouTube"
我读到了这个问题: iOS5中的YouTube - 完成按钮Tapped ,它提供了youtube api的链接:< a href =https://developers.google.com/youtube/player_parameters\"rel =nofollow noreferrer> https://developers.google.com/youtube/player_parameters
他们建议使用iframe。
youtube网站的示例是:
I read this question: Youtube in iOS5 - done button Tapped which gives the link to youtube api: https://developers.google.com/youtube/player_parameters
They recommend to use the iframe.
the example for youtube site is:
<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com"
frameborder="0"/>
我使用的代码:
<!DOCTYPE html>
<html>
<head>
<style>
* {
border:0;
margin:0;
}
</style>
</head>
<body>
<iframe webkit-playsinline id="player" type="text/html" width="320" height="180" src="http://www.youtube.com/embed/rEevIL1Wpcg?enablejsapi=1&playsinline=1&autoplay=1" frameborder="0">
</iframe>
</body>
</html>
有人可以帮我理解吗?我检查嵌入标志是否为真,它们都是允许在移动设备上播放的剪辑。
Can someone help me understand it? I check the embedded flag to be true, they are all clips that are allowed to play on mobile devices.
在设备上运行的视频示例:
Example for videos that works on the device:
- http://www.youtube.com/watch?v=rEevIL1Wpcg&feature=youtube_gdata
- http://www.youtube.com/watch?v=KzGe7pbGUiM
- http://www.youtube.com/watch?v=rEevIL1Wpcg&feature=youtube_gdata
- http://www.youtube.com/watch?v=KzGe7pbGUiM
设备上不能正常工作的视频示例并显示错误消息:
Example for videos that don't work on the device and bring up the error msg:
- http://www.youtube.com/watch?v=1vhFnTjia_I&feature = youtube_gdata
- http ://www.youtube.com/watch v = sxkiA0 IjBZ0& feature = youtube_gdata
- http://www.youtube.com/watch?v=1vhFnTjia_I&feature=youtube_gdata
- http://www.youtube.com/watch?v=sxkiA0IjBZ0&feature=youtube_gdata
推荐答案
您可以将webview用作youtube播放器
you can use webview as youtube player
尝试以下代码它适用于我
在.h文件中
@property (strong, nonatomic) UIWebView *webView;
和你的.m文件
NSString *videoURL = @"http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com";
// if your url is not in embed format or it is dynamic then you have to convert it in embed format.
videoURL = [videoURL stringByReplacingOccurrencesOfString:@"watch?v=" withString:@"embed/"];
NSRange range = [videoURLString rangeOfString:@"&"];
@try {
videoURLString = [videoURLString substringToIndex:range.location];
}
@catch (NSException *exception) {
}
// here your link is converted in embed format.
NSString* embedHTML = [NSString stringWithFormat:@"\
<html><head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe width=\"100%%\" height=\"240px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
</body></html>",videoURL];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:self.webView];
[self.webView loadHTMLString:embedHTML baseURL:nil];
您可以根据需要更改 webview框架,也可以更改 videoUrl 。
Here you can change webview frame as you want and also can change videoUrl.
这篇关于在iframe中使用YouTube嵌入播放器在iOS6中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!