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

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


当前回答

2018年12月,

寻找一个自动播放,循环,静音youtube视频的反应。

其他答案都不奏效。

我用一个库找到了一个解决方案:react-youtube

class Video extends Component {

    _onReady(event) {
        // add mute
        event.target.mute();
        // add autoplay
        event.target.playVideo();
    }

    render() {
        const opts = {
            width: '100%',
            height: '700px',
            playerVars: {
                // remove video controls 
                controls: 0,
                // remove related video
                rel: 0
            }
        };

        return (
            <YouTube
                videoId="oHg5SJYRHA0"
                opts={opts}
                // add autoplay
                onReady={this._onReady}
                // add loop
                onEnd={this._onReady}
            />
        )
    }

}

其他回答

您可以使用IFRAME和OBJECT嵌入的标志或参数都记录在这里;关于哪个参数适用于哪个玩家的细节也清楚地提到了:

YouTube嵌入式播放器和播放器参数

你会注意到所有的播放器(AS3, AS2和HTML5)都支持自动播放。

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

然后用jquery:

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

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

注意,你也可以在点击函数中使用这个来点击另一个元素来启动视频

更重要的是,你不能在移动设备上自动启动视频,所以用户总是要点击视频播放器本身来启动视频

编辑: 从文件上看,我不是百分之百确定。iframe已经准备好了,因为YouTube可能还在加载视频。我实际上是在点击函数中使用这个函数:

$('.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
});

1 -添加&enablejsapi=1到IFRAME SRC

jQuery func:

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

工作正常

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>
<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>