直到美国东部时间下午6点左右,下面的代码工作得很好,但现在由于某种原因,onStateChange没有触发。我在多个浏览器上尝试过,并让朋友检查过。看到什么明显的错误了吗?
<div id="video">
<div id="player_wrap">
<div id="player"></div>
</div>
</div>
<script>
var tag = document.createElement("script");
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onYouTubeIframeAPIReady() {
var player;
player = new YT.Player("player", {
width: 640,
height: 400,
videoId: "MbfsFR0s-_A",
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady() {
alert ("player ready");
}
function onPlayerStateChange(event) {
alert ("something happened");
}
这是iFrame Player API的临时问题。你可以在这里阅读:https://code.google.com/p/gdata-issues/issues/detail?id=4706
Jeff Posnick在这里发布了一个临时解决方案:http://jsfiddle.net/jeffposnick/yhWsG/3/
基本上,您只需要在onReady事件中添加事件侦听器(只是临时修复):function onReady() {
player.addEventListener('onStateChange', function(e) {
console.log('State is:', e.data);
});
}
张贴在这里的解决方案很好,真的很干净:
player = new YT.Player(element,{
events: {
onReady: function(){
/*
Do your stuff
*/
// Attach a void function to the event
player.addEventListener('onStateChange',function(){});
},
onStateChange: function(){
// Actual function that gets executed during the event
}
}
});