【发布时间】:2014-10-30 09:02:54
【问题描述】:
最近几天 YouTube 嵌入 API 出现了一个问题。问题是当您使用官方 API 嵌入视频时,它根本不允许您访问 API。当您尝试访问 API 时,您会在日志 (IOS) 上收到错误消息,如果您尝试通过 API 播放视频,视频会黑屏。如果通过 API 加载,但不使用 API,用户可以通过点击播放视频。
以下浏览器仍存在此问题:
iPad 和 iPhone 上的 iOS 7 Safari iPad 和 iPhone 上的 IOS 7 Chrome Android 4 铬
(我的播放按钮使用 API 播放视频并产生错误) JSfiddle:http://jsfiddle.net/frdd8nvr/6/
错误信息:
Unable to post message to https://www.youtube.com. Recipient has origin http://fiddle.jshell.net.
postMessage[native code]:0
Jwww-widgetapi.js:26:357
Nwww-widgetapi.js:25
(anonymous function)[native code]:0
html5player.js:1201:97
Blocked a frame with origin "https://www.youtube.com" from accessing a frame with origin "http://jsfiddle.net". The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match.
一些调试信息:
我看到 API 在网站上创建 iframe。 src 有时是 http,有时是 https。
我的测试表明,大多数时候 YouTube 服务器只是 LOCATION: https://... 对 https url 的请求,但大约 10% 的服务器为 http 请求提供了适当的内容。
我认为问题与强制 https 有关,但我无法找出解决方案。 你有同样的经历吗?你有解决这个问题的方法吗?是 YouTube 的错误吗?
我的测试代码:
<div id="myvideo"></div>
<button id="play-button">Play</button>
JS:
var tag = document.createElement("script");
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
onYouTubeIframeAPIReady = function () {
var vars = {
enablejsapi: 1,
origin: window.location.protocol + "//" + window.location.host,
autoplay: 0,
modestbranding: 1,
wmode: "opaque",
forceSSL: false
};
if (+(navigator.platform.toUpperCase().indexOf('MAC') >= 0 && navigator.userAgent.search("Firefox") > -1)){
vars.html5 = 1;
}
var playerobj = new YT.Player('myvideo', {
videoId: 'ZPy9VCovVME',
wmode: 'opaque',
playerVars: vars,
events: {
onReady: function(){
$('#play-button').on('click', function(){
playerobj.playVideo();
});
//playerobj.playVideo();
},
onStateChange: function(state){
switch(state.data){
case YT.PlayerState.PLAYING:
break;
//case YT.PlayerState.PAUSED:
case YT.PlayerState.ENDED:
break;
}
}
}
});
}
【问题讨论】:
-
我不确定,也许我不明白,但你的 jsfiddle 工作正常。你的意思是它只在 ios 上?
-
它在 IOS 和 Android 上不起作用,并且在我写的控制台上出现错误。对我来说,当 iframe 重定向到 https 时它似乎不起作用,但我无法找到如何防止重定向......也许你得到了来自不同服务器或运行在其上的不同服务器软件版本的响应。我认为这可能是因为有时我没有重定向到 https ..