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

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


当前回答

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

其他回答

这适用于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中设置“自动播放”。

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)都支持自动播放。

为了让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>

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

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