首页 文章

YouTube Iframe嵌入自动播放

提问于
浏览
192

我正在尝试嵌入YouTube视频的新iframe版本,并让它自动播放 .

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

12 回答

  • 6

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

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

    JavaScript API for iframe embeds存在,但仍作为实验性功能发布 .

    更新:现在完全支持iframe API,"Creating YT.Player objects - Example 2"显示了如何在JavaScript中设置"autoplay" .

  • 1

    youtube的嵌入代码默认关闭自动播放 . 只需在"src"属性的末尾添加 autoplay=1 即可 . 例如:

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

    自2018年4月以来,Google对Autoplay Policy进行了一些更改 . 所以你必须做这样的事情:

    <iframe src="https://www.youtube.com/embed/VIDEO_ID?autoplay=1" allow='autoplay'></iframe>
    
  • 1

    2014 iframe嵌入了如何使用自动播放嵌入YouTube视频,并且在剪辑结束时没有建议的视频

    rel=0&amp;autoplay
    

    示例如下: .

    <iframe width="640" height="360" src="//www.youtube.com/embed/JWgp7Ny3bOo?rel=0&amp;autoplay=1" frameborder="0" allowfullscreen></iframe>
    
  • 11

    在iframe src的末尾,添加 &enablejsapi=1 以允许在视频上使用js API

    然后使用jquery:

    jQuery(document).ready(function( $ ) {
      $('.video-selector iframe')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
    });
    

    这应该在document.ready上自动播放视频

    请注意,您也可以在点击功能中使用此功能点击其他元素以启动视频

    更重要的是,您无法在移动设备上自动启动视频,因此用户必须始终点击视频播放器本身才能启动视频

    编辑:我实际上并不是100%肯定在document.ready iframe准备就绪,因为YouTube仍然可以加载视频 . 我实际上是在click函数中使用这个函数:

    $('.video-container').on('click', function(){
      $('video-selector iframe')[0].contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
      // add other code here to swap a custom image, etc
    });
    
  • 39

    这里记录了可以与IFRAME和OBJECT嵌入一起使用的标志或参数;有关哪个参数适用于哪个播放器的详细信息也明确提到:

    YouTube Embedded Players and Player Parameters

    您会注意到所有玩家(AS3,AS2和HTML5)都支持 autoplay .

  • 7

    多个查询提示给那些不知道的人(过去我和未来的我)

    如果您使用网址进行单个查询 ?autoplay=1 正在工作as shown by mjhm's answer

    <iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1"></iframe>
    

    如果您正在进行多个查询,请记住第一个查询以 ? 开头,而其余查询以 & 开头

    假设您要关闭相关视频但启用自动播放...

    这很有效

    <iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?rel=0&autoplay=1"></iframe>
    

    这很有效

    <iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1&rel=0"></iframe>
    

    但这些都行不通..

    <iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?rel=0?autoplay=1"></iframe>
    
    <iframe src="https://www.youtube.com/embed/oHg5SJYRHA0&autoplay=1&rel=0"></iframe>
    

    example comparisons

    https://jsfiddle.net/Hastig/p4dpo5y4/

    more info

    阅读下面的NextLocal的回复,了解有关使用多个查询字符串的更多信息

  • 2

    要获得mjhm在2018年5月在Chrome 66上工作所接受的答案,我将 allow=autoplay 添加到iframe,将 enable_js=1 添加到查询字符串:

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

    要使用javascript api,

    <script type="text/javascript" src="swfobject.js"></script>
      <div id="ytapiplayer">
        You need Flash player 8+ and JavaScript enabled to view this video.
      </div>
    
      <script type="text/javascript">
    
        var params = { allowScriptAccess: "always" };
        var atts = { id: "myytplayer" };
        swfobject.embedSWF("http://www.youtube.com/v/OyHoZhLdgYw?enablejsapi=1&playerapiid=ytplayer&version=3",
                           "ytapiplayer", "425", "356", "8", null, null, params, atts);
    
      </script>
    

    要使用id播放youtube:

    swfobject.embedSWF
    

    参考:https://developers.google.com/youtube/js_api_reference magazine

  • 1

    1 - 将 &enablejsapi=1 添加到 IFRAME SRC

    2 - jQuery函数:

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

    工作良好

  • 10

    2018年8月我没有找到关于iframe实现的工作示例 . 其他问题只与Chrome有关,这给了它一点点 .

    您必须将声音静音 mute=1 才能在Chrome上自动播放 . FF和IE似乎使用 autoplay=1 作为参数正常工作 .

    <iframe src="//www.youtube.com/embed/{{YOUTUBE-ID}}?autoplay=1&mute=1" name="youtube embed" allow="autoplay; encrypted-media" allowfullscreen></iframe>
    
  • 6
    <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>
    

相关问题