【发布时间】:2016-10-23 10:23:20
【问题描述】:
我想自动播放我网站上静音的视频。我发现了一些似乎有效的代码,但它没有。为什么这段 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/
谢谢!
【问题讨论】:
-
您可以查看 YouTube API 文档吗?我确定有一个选项可以添加到
playerVars。
标签: javascript html youtube autoplay