javascript - How do I get the YouTube API to load onclick? - Stack Overflow

I am using the YouTube Iframe as shown below. I want to make my player load on the click of a button in

I am using the YouTube Iframe as shown below. I want to make my player load on the click of a button instead as soon as the API loads.

I have created a system where a user of my website will be able to choose what videos will be loaded into the playlist, so I cannot hardcode the YouTube video IDs. Is it possible to make an onclick function that fires an event that loads the player? If so, how??

Thanks so much!!!

<script>
  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');

  tag.src = "";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      events: {    //CAN I REPLACE THE ONREADY EVENT WITH ONE
        'onReady': onPlayerReady, //THAT FIRES ONCLICK? 
        'onStateChange': onPlayerStateChange 
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }

I am using the YouTube Iframe as shown below. I want to make my player load on the click of a button instead as soon as the API loads.

I have created a system where a user of my website will be able to choose what videos will be loaded into the playlist, so I cannot hardcode the YouTube video IDs. Is it possible to make an onclick function that fires an event that loads the player? If so, how??

Thanks so much!!!

<script>
  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');

  tag.src = "https://www.youtube./iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      events: {    //CAN I REPLACE THE ONREADY EVENT WITH ONE
        'onReady': onPlayerReady, //THAT FIRES ONCLICK? 
        'onStateChange': onPlayerStateChange 
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }
Share Improve this question asked Dec 12, 2016 at 22:54 Bastion_de_ParapluiBastion_de_Paraplui 311 silver badge2 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can do something like code below

var tag = document.createElement('script');
tag.src = "https://www.youtube./iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var video_id = '' //Desired video ID
var player;
$('LOAD_BUTTON').on('click', function() {
  // Load player when LOAD_BUTTON is clicked
  player = new YT.Player('player', {
    height: '390',
    width: '640',
    videoId: video_id,
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange 
    }
  });
});

function onPlayerReady(event) {
  $('PLAY_BUTTON').on('click', function() {
    // Play video when PLAY_BUTTON is clicked
    event.target.playVideo();
  });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744805469a4594749.html

相关推荐

  • javascript - How do I get the YouTube API to load onclick? - Stack Overflow

    I am using the YouTube Iframe as shown below. I want to make my player load on the click of a button in

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信