7

直到美国东部标准时间下午 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"); 

}
4

2 回答 2

6

这是 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);
    });
}
于 2013-06-13T04:39:35.353 回答
-1

此处发布的解决方案运行良好,并且非常干净:

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
        }
    }
});
于 2013-09-02T04:34:29.080 回答