JavaScript Make YouTube api automatically start another video when first video ended make youtube replay automatically,make youtube repeat automatically,how to

I need to make a video page for my website which must contain a video from youtube (https://www.youtube.com/watch?v=bWPMSSsVdPk) and after this video ended I need to automatically display a second video (https://www.youtube.com/watch?v=0afZj1G0BIE) and the div with class content

This is my code so far:

<style>
  .content {
    display: none;
  }
</style>

<div class="content">
  <button>Show this button after first video ends</button>
</div>

<div id="player"></div>

<script src="http://www.youtube.com/player_api"></script>
<script>
  // create youtube player
  var player;
  function onYouTubePlayerAPIReady() {
      player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'bWPMSSsVdPk',
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
      });
  }
  // autoplay video
  function onPlayerReady(event) {
      event.target.playVideo();
  }
  // when video ends
  function onPlayerStateChange(event) {        
      if(event.data === 0) {          
          alert('done');
      }
  }
</script>
Answer:1

There is no direct way as to how one can do this with the available set of parameters that a Youtube iframe Player API has. But you can try doing this.

  • Create a Playlist of the video's you want to play consecutively in the same order.
  • In the REST API call set the listType parameter as playlist.
  • Retrieve the playlistID of that particular playlist.

    http://www.youtube.com/embed?listType=playlist&list=PLPLAYLISTID
    
  • Finally, set the autoplay parameter to 1 so that the video's in the playlist play automatically.

Answer:2

I've found a solution for this but it seems that the sound of the second video is also playing in the same time with the first video...

<style>
    .second-video {
        display: none;
    }
</style>

<div class="video-site">
  <div class="first-video">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <div id="player"></div>
  </div>
  <div class="second-video">
    <div id="player2"></div>
  </div>
</div>

<script src="http://www.youtube.com/player_api"></script>
<script>
  // create youtube player
  var player;
  var player2;

  function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'qCDxEif_9js',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
    player2 = new YT.Player('player2', {
      height: '390',
      width: '640',
      videoId: '43jHG-yH9Gc',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  // autoplay video
  function onPlayerReady(event) {
    event.target.playVideo();
    $('.close').click(function(){
      $('.first-video').hide(3000);
      $('.second-video').fadeIn();
    });
  }
  // when video ends
  function onPlayerStateChange(event) {
    if(event.data === 0) {
      $('.first-video').hide(3000);
      $('.second-video').fadeIn();
    }
  }

</script>
Answer:3

I'm making a tip calculator and I would like the tip amount to display for the user to see. The problem I'm having is the output showing up as 'NaN' or 'undefined'. I've tried making changes to my ...

I'm making a tip calculator and I would like the tip amount to display for the user to see. The problem I'm having is the output showing up as 'NaN' or 'undefined'. I've tried making changes to my ...

  1. javascript input return key
  2. javascript input return event
  3. javascript input return
  4. javascript input return false
  5. javascript input return function
  6. javascript return input value
  7. javascript return input text
  8. javascript input text return key
  9. javascript input value return empty
  10. javascript input carriage return
  11. javascript input onkeypress return

I'm going through an array to apply different classes based on a state that is set externally. This is how I do it now, but I feel like i'm repeating myself a lot. Is there a DRYer way to do it? The ...

I'm going through an array to apply different classes based on a state that is set externally. This is how I do it now, but I feel like i'm repeating myself a lot. Is there a DRYer way to do it? The ...

  1. conditionally applying class
  2. conditionally apply class angular
  3. conditionally apply class react
  4. conditionally apply class angular 4
  5. conditionally apply class angular 7
  6. conditionally apply class vue
  7. conditionally apply class angularjs
  8. conditionally apply class in angular2
  9. conditionally apply class css

<div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Act</label> <input ...

<div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Act</label> <input ...

  1. jquery dynamically add rows form
  2. dynamically add rows to form

I've 1000's of users and user data looks like the below. Some users have the devices as [{some data}] (array), some users have devices as {some data}, and some users have devices as empty []. I ...

I've 1000's of users and user data looks like the below. Some users have the devices as [{some data}] (array), some users have devices as {some data}, and some users have devices as empty []. I ...