0

我在构建播放器时遵循了 Youtube API。我需要通过 javascript 调用自动播放函数,但它不会听 setTimeout 函数,也不会开始播放:

有人可以点亮我我哪里错了吗?

问候!

var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/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: {
        'onReady': onPlayerReady,
      }

    });
  }

  function onPlayerReady(event){
  setTimeout(function(){

  playVideo();

  },5000);

  }
4

1 回答 1

-1

当您声明player变量并稍后将其设置为 instance of 时YT.Player,您也必须使用相同的变量setTimeout

setTimeout(function(){ 
    player.playVideo();
},5000);
于 2016-10-29T08:38:45.713 回答