ios - Iframe 中的 YouTube 嵌入播放器在 iOS6 中不起作用

标签 ios objective-c iframe uiwebview youtube-api

我真的需要你的帮助。在 iOS 应用程序上工作。我想玩优酷。通过阅读许多博客和帖子,我了解到我们需要使用 iframe 才能播放 YouTube 视频。

但是,在某些视频中我得到:“此视频包含来自 XYZ 的内容。在某些网站上播放受到限制。在 YouTube 上观看

我读了这个问题:Youtube in iOS5 - done button Tapped它给出了 youtube api 的链接:https://developers.google.com/youtube/player_parameters 他们建议使用 iframe。
youtube 网站的例子是:

<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>

谁能帮我理解一下?我检查了嵌入式标志为真,它们都是允许在移动设备上播放的剪辑。

在设备上运行的视频示例:

视频在设备上无法播放并显示错误消息的示例:

最佳答案

你可以使用 webview 作为 youtube 播放器

尝试下面的代码它对我有用

在.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 frame,也可以更改videoUrl

关于ios - Iframe 中的 YouTube 嵌入播放器在 iOS6 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19339575/

相关文章:

javascript - 如何更改 Disqus 评论字段中的默认占位符文本?

html - IE、Iframe 和多个类

ios - 代码 8 : PAC Fetch failed with error [NSURLErrorDomain:-1003] log on Console window

ios - IOS7中的UDID替换

ios - super viewdidload 给我一个不可见的@interface for uitableview 为选择器 viewdidload 声明

ios - 尝试从 plist 中读取整数会导致意外结果

ios - 当选项卡栏不是 Root View Controller 时,无法更改 App Delegate 中的 UITabBarController 角标(Badge)

objective-c - 想要将3DES加密的NSData转换为NSString

video - 播放youtube视频后如何隐藏和观看以后的相关视频

ios - 创建自己的 UITableViewController