Html 将YouTube自动播放延迟20秒
是否可以延迟嵌入YouTube播放器的Html 将YouTube自动播放延迟20秒,html,youtube-api,Html,Youtube Api,是否可以延迟嵌入YouTube播放器的iframe自动播放 这是我的嵌入代码: <iframe width="560" height="315" src="https://www.youtube.com/embed/iejOAPyooXs?wmode=transparent&autoplay=1" frameborder="0" allowfullscreen></iframe> 您可以使用JavaScript来简化延迟。最初仅在页面上显示视频的缩略图。在Jav
iframe
自动播放
这是我的嵌入代码:
<iframe width="560" height="315" src="https://www.youtube.com/embed/iejOAPyooXs?wmode=transparent&autoplay=1" frameborder="0" allowfullscreen></iframe>
您可以使用JavaScript来简化延迟。最初仅在页面上显示视频的缩略图。在JavaScript中,等待20秒,然后用视频iframe替换预览
另一篇文章介绍了如何只显示缩略图:。使用youtube播放器API更新了我的答案,以获得更多选项和动态输出。它有许多选项可供选择,您可以使用Javascript轻松控制它。我希望这能有所帮助。@wasikuss发布答案的积分
// Load the IFrame Player API code asynchronously.
setTimeout(function() {
player.playVideo();
}, 20000);
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '315',
width: '560',
videoId: 'iejOAPyooXs'
});
}
//异步加载IFrame播放器API代码。
setTimeout(函数(){
player.playVideo();
}, 20000);
var tag=document.createElement('script');
tag.src=”https://www.youtube.com/player_api";
var firstScriptTag=document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(标记,firstScriptTag);
//用and替换“ytplayer”元素
//YouTube播放器的API代码下载后。
var播放器;
函数onYouTubePlayerAPIReady(){
player=新的YT.player('ytplayer'{
高度:'315',
宽度:“560”,
videoId:“IEJOAPYOXS”
});
}
参考资料Sildoreth的解决方案有一个陷阱:用户不能自己播放视频。 更合适的解决方案是使用YouTube IFrame API()。20秒后,只需检查视频状态(
player.getPlayerState()
),如果它是视频提示(5
),请调用player.playVideo()
可能重复OK,谢谢你的想法,我会尝试这两种解决方案,但我非常确定,在我用JavaScript重新制作之后,这会更简单。没问题。鉴于用户可以选择播放视频或等待视频自动播放,但这取决于您需要什么。很乐意帮忙。:)但若用户自己点击播放按钮,那个么视频将重新启动,因为src
更改了更新的答案,希望它能有所帮助