我正在尝试使用来自父级的 postMessage 命令来控制 YouTube iframe,但它似乎不起作用。
由于多种原因,我没有使用 YouTube API,只是使用带有 YouTube 嵌入视频的普通 iframe。
<iframe id="video-player" :src="'https://www.youtube.com/embed/' + code + '?autoplay=1'"
seamless sandbox="allow-scripts allow-same-origin allow-presentation"></iframe>
我尝试发送命令的方式是:
var iframe = document.getElementById('video-player');
iframe.contentWindow.postMessage(JSON.stringify(
{ event: 'command', func: 'pauseVideo' }), 'https://www.youtube.com');
看来 iframe 已被正确选择,但我不确定是否正在发送 postMessage 命令,因为视频会忽略它们。
我究竟做错了什么?
我找到了解决方案。 YouTube 网址需要查询参数“enablejsapi=1”。
<iframe id="video-player" :src="'https://www.youtube.com/embed/' + code + '?autoplay=1&enablejsapi=1'"
seamless sandbox="allow-scripts allow-same-origin allow-presentation"></iframe>
像这样它正确地监听 postMessage() 命令。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)