Youtube player api with loop

Prithviraj Mitra picture Prithviraj Mitra · Oct 16, 2013 · Viewed 35.6k times · Source

I am struggling with setting up the loop for youtube videos using youtube player api.

The problem is that the video is not running under a loop.

Here is my code

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',

          playerVars: {
            'controls': 0,           
            'showinfo': 0,
            'rel': 0,
            'loop': 1

          },
          videoId: 'qzZuBWMnS08',
          events: {
            'onReady': onPlayerReady,
           // 'onStateChange': onPlayerStateChange
          }
        });
      }
function onPlayerReady(event) {
        //  event.target.setLoop(true);
        event.target.playVideo();
      }

The loop:1 doesn't seem to be working.Also is there any way to remove the share and video title from the top of the video.

Thanks in advance.

Answer

DanielM picture DanielM · May 7, 2015

If the video isn't changing, you can just do

onStateChange: 
    function(e) {
        if (e.data === YT.PlayerState.ENDED) {
            player.playVideo(); 
        }
    }

This will prevent unnecisarily reloading the video