gpt4 book ai didi

javascript - YouTube 自动播放并静音

转载 作者:行者123 更新时间:2023-11-28 05:25:37 30 4
gpt4 key购买 nike

我想自动播放我网站上静音的视频。我发现一些代码似乎有效,但事实并非如此。为什么这段 JavaScript 和 HTML 代码不起作用?

<div id="player"></div>

<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
playerVars: { 'autoplay': 1, 'controls': 1,'autohide':1,'wmode':'opaque' },
videoId: 'RDfjXj5EGqI',
events: {
'onReady': onPlayerReady}
});
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.mute();
} </script>

jsFiddle 用于测试:https://jsfiddle.net/qswpr8tL/

谢谢!

最佳答案

请像这样构建您的 onReady 处理程序:

var player;
// ...
function onPlayerReady(event) {
player.mute();
}

根据documentation您必须在 player 对象上调用 mute 方法。

关于javascript - YouTube 自动播放并静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40201872/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com