How do you mute an embedded Youtube player?

Jonathan picture Jonathan · Jul 21, 2014 · Viewed 76.8k times · Source

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.

Answer

Jonathan picture Jonathan · Jul 21, 2014

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