【问题标题】:YouTube autoplay with muteYouTube 自动播放静音
【发布时间】:2016-10-23 10:23:20
【问题描述】:

我想自动播放我网站上静音的视频。我发现了一些似乎有效的代码,但它没有。为什么这段 javascript 和 HTML 代码不起作用?

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

<script>
  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "http://www.youtube.com/player_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 onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
      playerVars: { 'autoplay': 1, 'controls': 1,'autohide':1,'wmode':'opaque' },
      videoId: 'RDfjXj5EGqI',
      events: {
        'onReady': onPlayerReady}
    });
  }

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

用于测试的jsFiddle:https://jsfiddle.net/qswpr8tL/

谢谢!

【问题讨论】:

  • 您可以查看 YouTube API 文档吗?我确定有一个选项可以添加到playerVars

标签: javascript html youtube autoplay


【解决方案1】:

请像这样构建您的 onReady 处理程序:

var player;
// ...
function onPlayerReady(event) {
  player.mute();
}

根据documentation,您必须在player 对象上调用mute 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 2014-02-21
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2017-06-22
    相关资源
    最近更新 更多