title: YouTube API When video is done
body: <p>I am using YouTube API and I am trying to set-up an onStateChange when my video ends to shuffle my playlist and start at 0...this is what I got thus far</p>
  
  <pre><code> var player;
        function onYouTubeIframeAPIReady() {
          player = new YT.Player('player', {
            height: '390',
            width: '640',
            events: {
              'onReady': onPlayerReady,
              'onStateChange': onPlayerStateChange
            }
  
          });
        }
  
  
  
  
  
        function onPlayerReady(event) {
          player.loadPlaylist({'listType': 'playlist', 'list': 'PLF776D41D8211F340','index': '0','startSeconds': '0','suggestedQuality': 'hd720'});
        }
  
        var done = false;
        function onPlayerStateChange(event) {
          if (event.data == YT.PlayerState.PLAYING &amp;&amp; done == false) {
              player.setShuffle(true);
              player.playVideoAt(0);
            done = true;
          }else if (event.data == YT.PlayerState.ENDED &amp;&amp; done == true) {
              player.setShuffle(true);
              player.playVideoAt(0);
            done = false;
          }
  
        }
  </code></pre>
  
  <p>What Am I doing wrong here?</p>