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

2022-01-19 00:00:00 youtube youtube-api javascript

我正在尝试使用 Youtube 播放器,但我无法让它默认静音.

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);
}

如何从一开始就静音?

小提琴

更新:

JavaScript Player API 已弃用.
请改用 iframe 嵌入.

推荐答案

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

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();
}

小提琴

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

相关文章