你如何将嵌入式Youtube播放器静音?

时间:2014-07-21 14:54:26

标签: javascript youtube youtube-api

我正在尝试使用Youtube播放器,但默认情况下我无法将其静音。

function onPlayerReady() {
    player.playVideo();
    // Mute?!
    player.mute();
    player.setVolume(0);
}

如何从一开始就将其静音?

Fiddle


更新

JavaScript Player API is deprecated
请改用iframe Embeds

4 个答案:

答案 0 :(得分:24)

结果player.mute()工作正常。它只需要参数enablejsapi=1。小提琴中的初始测试不起作用,因为玩家启动有错误。以下作品。

HTML:

<iframe id="ytplayer" type="text/html" src="https://www.youtube-nocookie.com/embed/zJ7hUvU-d2Q?rel=0&enablejsapi=1&autoplay=1&controls=0&showinfo=0&loop=1&iv_load_policy=3" frameborder="0" allowfullscreen></iframe>

JS:

var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('ytplayer', {
        events: {
            'onReady': onPlayerReady
        }
    });
}

function onPlayerReady(event) {
    player.mute();
    player.playVideo();
}

<强> Fiddle

感谢Gagandeep Singh和Anton King指向enablejsapi=1

答案 1 :(得分:8)

以上所有答案因某些原因对我不起作用。我不得不在Youtube API上使用或折旧方法可能是奇怪的wordpress主题,我不确定。使播放器静音的唯一方法是将下面的代码插入标签。

// Loads 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);

  // Replaces the 'ytplayer' element with an <iframe> and
  // YouTube player after the API code downloads.
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
        height: '390',
        width: '640',
        videoId: 'YOUR_VIDEO_ID',
        playerVars: {
          autoplay: 1,
          controls: 1,
          disablekb: 1,
          hl: 'ru-ru',
          loop: 1,
          modestbranding: 1,
          showinfo: 0,
          autohide: 1,
          color: 'white',
          iv_load_policy: 3,
          theme: 'light',
          rel: 0
        },
        events: {
            'onReady': onPlayerReady,
        }
    });
  }

function onPlayerReady(event){
    player.mute();
}
<div id="ytplayer"></div>

答案 2 :(得分:2)

值得注意的是,YouTube API要求您直接在<script>标记内或通过标准document.onLoad()本机侦听器而不是命名函数在标记内运行此API。

否则它本身不会将onYouTubeIframeAPIReady()函数绑定到DOM。

答案 3 :(得分:1)

尝试以下代码

var youtubeplayer = iframe.getElementById('ytplayer');
youtubeplayer .setVolume(0);

以下是您的小提琴更新版本,

注意:必须在视频网址中包含enablejsapi = 1

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

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

    var player;

    function onYouTubeIframeAPIReady() {
        player = new YT.Player('ytplayer', {
            events: {
                'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
            }
        });
    }

    function onPlayerReady(event) {
        player.playVideo();
        // Mute?!
        //player.mute(); instead of this use below
        event.target.mute();
        //player.setVolume(0);
    }

DEMO 希望这会有所帮助...