How do you mute an embedded Youtube player?

后端 未结 4 609
闹比i
闹比i 2020-11-28 12:13

I\'m experimenting with the Youtube player but I can\'t get it to mute by default.

function onPlayerReady() {
    player.playVideo();
    // Mute?!
    playe         


        
相关标签:
4条回答
  • 2020-11-28 12:40

    All above answers didn't work for me for some reason. It might be weird wordpress theme that I had to use or depreciated methods at Youtube API, I'm not sure. The only way of muting the player was to insert the code below into tag.

    // 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>

    0 讨论(0)
  • 2020-11-28 12:45

    Try below code

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

    And below is your fiddle updated version,

    NOTE: Must include enablejsapi=1 in video url

            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 Hope this helps...

    0 讨论(0)
  • 2020-11-28 12:46

    It's important to note that YouTube API mandates you run this within your markup directly within a <script> tag, or via a standard document.onLoad() native listener and not as a named function.

    Otherwise it will not natively bind the onYouTubeIframeAPIReady() function to the DOM.

    0 讨论(0)
  • 2020-11-28 12:49

    Turns out player.mute() works fine. It only needed the parameter enablejsapi=1. Initial test in the fiddle didn't work because the player initiation had an error. The following works.

    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

    Credit to Gagandeep Singh and Anton King for pointing to enablejsapi=1

    0 讨论(0)
提交回复
热议问题