title: Youtube Player API Ended state never fired
body: <p>Hi so i have implemented the Youtube Player API and based on the event i am doing certain things, it is working perfectly for when the video is playing or when it is paused however when the video is ended the ended event is never called, instead it recalls the paused event. I get the url for the video using a get from the url bar, this is working perfectly.</p>
  
  <pre><code>&lt;script&gt;
  function get(name){
      if(name=(new RegExp('[?&amp;]'+encodeURIComponent(name)+'=([^&amp;]*)')).exec(location.search))
        return decodeURIComponent(name[1]);
  }
  
  //Load player api asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  var player;
  function onYouTubeIframeAPIReady() {
      player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: get('url'),
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
      });
  }
  function onPlayerReady(evt) {
     // evt.target.playVideo();
  }
  function onPlayerStateChange(evt) {
      if (evt.data == YT.PlayerState.PLAYING) {
          $.ajax({ url:'insertToDBFromVid.php',
              data: {action : 'playing' },
              type: 'post',
              done: function(result){
                  alert(result);
                  console.log("Success, Playing");
              },
              error: function(result){
                  alert(result)
                  console.log("Error in Playing");
              }});
  
      }
      if (evt.data == YT.PlayerState.ENDED){
          //done=true;
          //console.log("Ended called!");
          $.ajax({ url:'insertToDBFromVid.php',
              data: {action : 'ended' },
              type: 'post',
              done: function(result){
                  alert(result);
                  console.log("Success, Ended");
              },
              error: function(result){
                  alert(result)
                  console.log("Error in Ended");
              }});
      }       
      if (evt.data == YT.PlayerState.PAUSED) {
          $.ajax({ url:'insertToDBFromVid.php',
              data: {action : 'paused' },
              type: 'post',
              done: function(result){
                  alert(result);
                  console.log("Success, Paused");
              },
              error: function(result){
                  alert(result)
                  console.log("Error in Paused");
              }});
      }
  
  }
  function stopVideo() {
      player.stopVideo();
  }
  </code></pre>
  
  <p> </p>
  
  <p>If anyone can shine a light onto why this is happening it would be greatly appreciated :)</p>