22

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

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

How do I mute it from the start?

Fiddle


Update:

JavaScript Player API is deprecated.
Use iframe Embeds instead.

Jonathan
  • 8,771
  • 4
  • 41
  • 78

4 Answers4

27

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

Jonathan
  • 8,771
  • 4
  • 41
  • 78
  • Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://youtube.com') does not match the recipient window's origin ('https://www.youtube.com'). – Suisse Dec 02 '15 at 16:38
  • This still doesn't work for me. And your demo plays sound. – YoTengoUnLCD May 25 '16 at 18:07
  • 3
    The jsfiddle doesn't mute the video. – Alvaro Jun 13 '16 at 11:15
  • 6
    [Here's the working fiddle](http://jsfiddle.net/BFDKS/966/) with a real youtube video and not the `youtube-nocookie` one used in here – Alvaro Jun 13 '16 at 11:20
11

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>
Artem Kiselev
  • 643
  • 1
  • 7
  • 7
3

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.

Paul Kane
  • 111
  • 1
  • 2
1

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

Anto king
  • 1,222
  • 1
  • 13
  • 26