YouTube Iframe API - OnStateChange突然无法正常工作

时间:2013-06-13 03:59:58

标签: youtube-api

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

}

2 个答案:

答案 0 :(得分: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);
    });
}

答案 1 :(得分:-1)

发布的解决方案here运行正常且非常干净:

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
        }
    }
});