我试图嵌入新的iframe版本的YouTube视频,并让它自动播放。
据我所知,没有办法通过修改URL的标志来做到这一点。有办法通过使用JavaScript和API来做到这一点吗?
我试图嵌入新的iframe版本的YouTube视频,并让它自动播放。
据我所知,没有办法通过修改URL的标志来做到这一点。有办法通过使用JavaScript和API来做到这一点吗?
当前回答
youtube的内嵌代码默认是自动播放的。只需在“src”属性的末尾添加autoplay=1。例如:
<iframe src="http://www.youtube.com/embed/xzvScRnF6MU?autoplay=1" width="960" height="447" frameborder="0" allowfullscreen></iframe>
其他回答
我没有找到iframe实现的工作示例。 其他问题只与Chrome有关,这透露了一点。
为了在Chrome上自动播放,你必须静音声音mute=1。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>
现在我在iframe标签上添加了一个新的属性“allow”,例如:
允许= "加速度计;播放;加密介质;陀螺仪; 画中画”
最终代码是:
<iframe src="https://www.youtube.com/embed/[VIDEO-CODE]?autoplay=1"
frameborder="0" style="width: 100%; height: 100%;"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"></iframe>
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}
/>
)
}
}
自2018年4月起,谷歌对自动播放政策进行了一些更改。你不仅需要添加autoplay=1作为查询参数,还需要添加allow='autoplay'作为iframe的属性
所以你必须这样做:
<iframe src="https://www.youtube.com/embed/VIDEO_ID?autoplay=1" allow='autoplay'></iframe>
为那些不知道(过去的我和未来的我)的人提供多个查询提示
如果您只是使用url进行单个查询?autoplay=1工作,如mjhm的答案所示
<iframe src="https://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1"></iframe>
如果你有多个查询还记得第一个以a开头吗?而其他的则以&开头
假设你想关闭相关视频,但启用自动播放…
这是
<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>
例子比较
https://jsfiddle.net/Hastig/p4dpo5y4/
更多信息
有关使用多个查询字符串的更多信息,请阅读下面NextLocal的回复