Javascript 带静音的YouTube自动播放
我想在我的网站上自动播放静音的视频。我找到了一些似乎有效的代码,但却没有。为什么这个javascript和HTML代码不能工作Javascript 带静音的YouTube自动播放,javascript,html,youtube,autoplay,Javascript,Html,Youtube,Autoplay,我想在我的网站上自动播放静音的视频。我找到了一些似乎有效的代码,但却没有。为什么这个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://w
<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>
// 2. 此代码异步加载IFrame播放器API代码。
var tag=document.createElement('script');
tag.src=”http://www.youtube.com/player_api";
var firstScriptTag=document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(标记,firstScriptTag);
// 3. 此函数用于创建(和YouTube播放器)
//API代码下载后。
var播放器;
函数onYouTubePlayerAPIReady(){
player=新的YT.player('player'{
playerBars:{'autoplay':1,'controls':1,'autohide':1,'wmode':'opaque'},
videoId:'RDfjXj5EGqI',
活动:{
“onReady”:onPlayerReady}
});
}
// 4. 当视频播放器准备就绪时,API将调用此函数。
函数onPlayerReady(事件){
event.target.mute();
}
用于测试的JSFIDLE:
谢谢 请像这样构建onReady处理程序:
var player;
// ...
function onPlayerReady(event) {
player.mute();
}
根据需要,您必须在
player
对象上调用mute
方法 请像这样构建onReady处理程序:
var player;
// ...
function onPlayerReady(event) {
player.mute();
}
根据需要,您必须在
player
对象上调用mute
方法 你能检查一下YouTube API文档吗?我确信有一个选项可以添加到playerVars
。您可以查看YouTube API文档吗?我确信有一个选项可以添加到playerVars
。