如何静音嵌入式 youtube 视频?音量 = 0 不起作用

How to mute embeded youtube video? Volume = 0 doesn't work


volume=0 似乎不再受支持。
有谁知道我现在如何在 2017 年将我的 iframe 静音?

当我点击取消静音图标时,静音图标应该出现并且视频应该被静音。 (反之亦然)

这是我目前为止尝试过的:

//Mute-Unmute Music
 $('#unmute').on('click', function() {
    $("#unmute").hide();
    $("#mute").show();
    $("#iframe").attr("volume", "0");
 });
 $('#mute').on('click', function() {
    $("#mute").hide();
    $("#unmute").show();
    $("#iframe").attr("volume", "1");
  });

Any web page that uses the IFrame API must also implement the following JavaScript function:

onYouTubeIframeAPIReady – The API will call this function when the page has finished downloading the JavaScript for the player API, which enables you to then use the API on your page. Thus, this function might create the player objects that you want to display when the page loads.

因此您的代码应如下所示:

var player;

function onYouTubeIframeAPIReady() {
   player = new YT.Player('ytplayer', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE'
     }
  });
}

$('#unmute').on('click', function() {
    $("#unmute").hide();
    $("#mute").show();
    player.mute();
});
$('#mute').on('click', function() {
    $("#mute").hide();
    $("#unmute").show();
    player.unMute()
});