Javascript 无铬和自动播放不工作

Javascript 无铬和自动播放不工作,javascript,youtube-api,Javascript,Youtube Api,无法让自动播放的无铬播放器工作。我正在尝试的代码是直接从developers.google.com文档中复制的: IFRAME示例: JS示例: //异步加载IFrame播放器API代码。 var tag=document.createElement('script'); tag.src=”https://www.youtube.com/player_api"; var firstScriptTag=document.getElementsByTagName('script')[0];

无法让自动播放的无铬播放器工作。我正在尝试的代码是直接从developers.google.com文档中复制的:

IFRAME示例:


JS示例:


//异步加载IFrame播放器API代码。
var tag=document.createElement('script');
tag.src=”https://www.youtube.com/player_api";
var firstScriptTag=document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(标记,firstScriptTag);
//用and替换“ytplayer”元素
//YouTube播放器的API代码下载后。
var播放器;
函数onYouTubePlayerAPIReady(){
player=新的YT.player('ytplayer'{
高度:'405',
宽度:“720”,
videoId:'M7lc1UVf VE',
自动播放:1,
控件:0,
循环:1,
rel:0,
showinfo:0,
主题:“光”
});
}

使用iFrame API时,任何播放器对象参数(即,如果要将播放器作为iFrame而不是API嵌入,则可以作为URL参数附加的参数)都需要设置为PlayerBars参数对象的一部分,而不是作为YT.player参数的直接参数。具体来说,您的代码应该如下所示:

<div id="ytplayer"></div>

<script>
  // Load the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // Replace the 'ytplayer' element with an <iframe> and
  // YouTube player after the API code downloads.
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
     height: '405',
     width: '720',
     videoId: 'M7lc1UVf-VE',
     playerVars: {
      autoplay:1,
      controls:0,
      loop:1,
      rel:0,
      showinfo:0,
      theme:'light'
      }
    });
  }
</script>

//异步加载IFrame播放器API代码。
var tag=document.createElement('script');
tag.src=”https://www.youtube.com/player_api";
var firstScriptTag=document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(标记,firstScriptTag);
//用and替换“ytplayer”元素
//YouTube播放器的API代码下载后。
var播放器;
函数onYouTubePlayerAPIReady(){
player=新的YT.player('ytplayer'{
高度:'405',
宽度:“720”,
videoId:'M7lc1UVf VE',
playerVars:{
自动播放:1,
控件:0,
循环:1,
rel:0,
showinfo:0,
主题:“光”
}
});
}
<div id="ytplayer"></div>

<script>
  // Load the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // Replace the 'ytplayer' element with an <iframe> and
  // YouTube player after the API code downloads.
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
    height: '405',
    width: '720',
    videoId: 'M7lc1UVf-VE',
    autoplay:1,
    controls:0,
    loop:1,
    rel:0,
    showinfo:0,
    theme:'light'
    });
  }
</script>
<div id="ytplayer"></div>

<script>
  // Load the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // Replace the 'ytplayer' element with an <iframe> and
  // YouTube player after the API code downloads.
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
     height: '405',
     width: '720',
     videoId: 'M7lc1UVf-VE',
     playerVars: {
      autoplay:1,
      controls:0,
      loop:1,
      rel:0,
      showinfo:0,
      theme:'light'
      }
    });
  }
</script>