Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube Iframe API - OnStateChange suddenly not working [duplicate]

Tags:

youtube-api

Until around 6pm EST this code below would have worked fine, but now for some reason onStateChange is not firing. I've tried on multiple browsers and have had friends check it. See anything obviously wrong?

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

}
like image 474
Jon Lachonis Avatar asked Jun 13 '13 03:06

Jon Lachonis


1 Answers

This is a temporary issue with the iFrame Player API. You can read about it here: https://code.google.com/p/gdata-issues/issues/detail?id=4706

Jeff Posnick posted a temporary workaround here: http://jsfiddle.net/jeffposnick/yhWsG/3/

Basically, you just need to add the event listener within the onReady event (just a temporary fix):

function onReady() {
    player.addEventListener('onStateChange', function(e) {
        console.log('State is:', e.data);
    });
}
like image 193
Matt Koskela Avatar answered Nov 16 '22 17:11

Matt Koskela