【发布时间】:2020-06-28 21:14:00
【问题描述】:
我希望嵌入 youtube 的视频在加载后自动播放,但在视频网址似乎不起作用后使用此视频
?autoplay=1&loop=1&autopause=0&muted=1
我在 iframe 中显示从用户输入检索到的视频,如下所示
<iframe width="560" height="315" src="//www.youtube.com/embed/${videoId}?autoplay=1&loop=1&autopause=0&muted=1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
</iframe>`
我还有一个获取 videoId 的函数
function getId(url) {
const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/;
const match = url.match(regExp);
return (match && match[2].length === 11) ?
match[2] :
null;
}
视频已正确显示,但不会自动开始。我不知道是哪部分代码导致了这个问题
【问题讨论】:
-
请将 muted = 1 删除为 mute=1。工作示例jsfiddle.net/nishantj/36ge4vtL
标签: javascript html youtube