如何让嵌入的Youtube视频自动播放?

41
在我的项目中,有一个视频库模块。在该模块中,有两个选项:直接上传FLV和添加来自YouTube的视频嵌入代码。
我正在为一个div元素编写一些嵌入代码(实际上,嵌入代码来自数据库)。我希望这个视频可以自动开始播放。
<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/zGPuazETKkI" frameborder="0" allowfullscreen></iframe>

这是从YouTube获取的示例嵌入代码。我想使用JavaScript使该视频自动播放。


8
请记住,大多数人不希望在未经同意的情况下听到/看到音频/视频广告。我们讨厌这种广告并希望它们消失得干干净净。 - Wayne Werner
自动播放功能已经不再普遍可用了。 - Jonathan DS
4个回答

73

你必须使用

<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/zGPuazETKkI?autoplay=1" frameborder="0" allowfullscreen></iframe>

?autoplay=1

而不是

&autoplay=1

它是第一个URL参数,因此会在问号后添加。


1
谢谢,这对我有用!这是插入在来自YouTube的嵌入代码之后的。;autoplay=1" 这是原始代码:<iframe width="560" height="315" src="http://www.youtube.com/embed/_5nrEUdCfd4?rel=0" frameborder="0" allowfullscreen></iframe> 当编辑插入到 "rel=0" 之后时,它起作用了。 - chuck
我唯一能让它工作的方法是删除 rel=0 参数。(反正我也不知道那是干嘛的)。 - Tony Beninate
1
rel=0会移除视频末尾的相关推荐视频。 - Katharine Osborne

7
这对我来说非常有效,你可以尝试在链接的末尾加上 ?rel=0&autoplay=1。
<iframe width="631" height="466" src="https://www.youtube.com/embed/UUdMixCYeTA?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>

4
<iframe title='YouTube video player' class='youtube-player' type='text/html'
        width='030' height='030'
        src='http://www.youtube.com/embed/ZFo8b9DbcMM?rel=0&border=&autoplay=1'
        type='application/x-shockwave-flash'
        allowscriptaccess='always' allowfullscreen='true'
        frameborder='0'></iframe>

只需在embed/后插入您的代码即可。


3
&autoplay=1添加到您的语法中,像这样
<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/zGPuazETKkI&amp;autoplay=1" frameborder="0" allowfullscreen></iframe>

7
根据相关问题,您需要添加“?autoplay=1”。如同 URL 参数的标准格式,第一个参数使用问号“?”标识,后面的参数使用“&”符号连接。 - blo0p3r

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接