uwe
uwe

Reputation: 4077

Play a 2nd youtube video after first one has finished playing

I have embedded youtube videos on my site. After the embedded video has finished playing I would like to immediately autoplay another youtube video. How could I do that?

Upvotes: 0

Views: 1311

Answers (1)

Leo
Leo

Reputation: 1505

When you initialize your youtube player add the onStateChange Event. Then see what the event was and if it was a PlayerState.Ended then you load the next video. something like this

    this.player = new YT.Player('player', {
      height: pxHeight,
      width: pxWidth,
      playerVars: {
                                'rel': 0,
                                'controls': 0,
                                'fs':0
      },
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange,
        'onError': onError
      }
    });

  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
        LOG("Youtube Playing");
    } else if (event.data == YT.PlayerState.ENDED) {
        LOG(">>>Youtube Ended");
        this.player.loadVideoById(ID, 0, "default");
    } else if (event.data == YT.PlayerState.PAUSED) {
        LOG("Youtube Paused");
    } else if (event.data == YT.PlayerState.BUFFERING) {
        LOG("Youtube Buffering");
    } else if (event.data == YT.PlayerState.CUED) {
        LOG("Youtube Cued");
    }
  }

Upvotes: 1

Related Questions