因此,我将 youtube iframe api 添加到我的网站并按照说明进行操作;这是我的代码:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
controls: '0',
autohide: '1',
videoId: 'I'd rather not display that',
events: {
// 'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
将控件设置为 0 不应按照 this article 显示玩家的控件但它仍然显示;
此外,将自动隐藏设置为 1 应该会使进度条和控件在几秒钟后滑出,如 this article 所示。但这也行不通。
我做错了什么吗?
最佳答案
我得到了嵌入版本和 iframe 版本,它们都可以自动播放、自动隐藏(实际上不需要)和隐藏控件。我使用了示例代码并添加了您需要的参数。
您所需要做的就是替换您的电影 ID,我想您就可以正常运行了。
这是 jsfiddle 链接,您可以在其中查看它的工作原理并获取代码。
嵌入版本:http://jsfiddle.net/franktudor/jk9SS/
<object width="640" height="390">
<param name="movie"
value="https://www.youtube.com/v/M7lc1UVf-VE?version=3&autoplay=1&autohide=1&controls=0"></param>
<param name="allowScriptAccess" value="always"></param>
<embed src="https://www.youtube.com/v/M7lc1UVf-VE?version=3&autoplay=1&autohide=1&controls=0"
type="application/x-shockwave-flash"
allowscriptaccess="always"
width="640" height="390"></embed>
</object>
这是 iFrame 版本:http://jsfiddle.net/franktudor/2wh8T/
<iframe id="ytplayer"
type="text/html"
width="640"
height="390"
src="http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&autohide=1&controls=0"
frameborder="0"/>
这两个选项都可以使用。您正在使用 iFrame 版本...我也推荐该版本。但如果您需要其他解决方案,可以使用嵌入版本。
它不是那种脚本风格,但如果需要,您可以使用包装器。
Here is a link to an HTML5 youtube (and vimeo) video wrapper.
好的,这是我工作的功能代码,如您的示例:http://jsfiddle.net/franktudor/DU57E/
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_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 onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
playerVars: { 'autoplay': 1, 'controls': 0 }, //this is what you need...
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
关于youtube-api - YouTube 播放器控件和自动隐藏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23378703/