我正在尝试使用来自父级的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命令,因为视频忽略了它们。
¿我做错了什么?
答案 0 :(得分:1)
我找到了解决方案。 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()命令。