我试图嵌入新的iframe版本的YouTube视频,并让它自动播放。

据我所知,没有办法通过修改URL的标志来做到这一点。有办法通过使用JavaScript和API来做到这一点吗?


当前回答

这适用于Chrome,但不适用于Firefox 3.6(警告:RickRoll视频):

<iframe width="420" height="345" src="http://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1" frameborder="0" allowfullscreen></iframe>

iframe嵌入的JavaScript API是存在的,但是仍然作为一个实验特性发布。

更新:iframe API现在完全支持和“创建YT。播放器对象-示例2”展示了如何在JavaScript中设置“自动播放”。

其他回答

youtube的内嵌代码默认是自动播放的。只需在“src”属性的末尾添加autoplay=1。例如:

<iframe src="http://www.youtube.com/embed/xzvScRnF6MU?autoplay=1" width="960" height="447" frameborder="0" allowfullscreen></iframe>

这适用于Chrome,但不适用于Firefox 3.6(警告:RickRoll视频):

<iframe width="420" height="345" src="http://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1" frameborder="0" allowfullscreen></iframe>

iframe嵌入的JavaScript API是存在的,但是仍然作为一个实验特性发布。

更新:iframe API现在完全支持和“创建YT。播放器对象-示例2”展示了如何在JavaScript中设置“自动播放”。

为了让mjhm在2018年5月在Chrome 66上工作,我在iframe中添加了allow=autoplay,并在查询字符串中添加了enable_js=1:

<iframe allow=autoplay width="420px" height="345px" src="http://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1&enable_js=1"></iframe>

1 -添加&enablejsapi=1到IFRAME SRC

jQuery func:

  $('iframe#your_video')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');

工作正常

<iframe width="560" height="315" 
        src="https://www.youtube.com/embed/9IILMHo4RCQ?rel=0&amp;controls=0&amp;showinfo=0&amp;autoplay=1" 
        frameborder="0" allowfullscreen></iframe>