通过 js 停止 youtube-iframe-video

stop youtube-iframe-video via js

如何在点击时停止 (youtube) 视频?我已经尝试了很多,但我无法让它工作……我做错了什么?关于互联网上的文档和数百篇帖子,这应该很有魅力……但事实并非如此:

html:

<iframe width="420" height="315" src="https://www.youtube.com/embed/2BagaJFimzk?autohide=1&showinfo=0&rel=0&enablejsapi=1&origin=http%3A%2F%2Fjsfiddle.net" frameborder="0" allowfullscreen></iframe>
<button>please stop :(</button>

在 js 中(加载 jquery 和 youtube-api):

var thisvid;

function onYouTubePlayerAPIReady() {
    thisvid = new YT.Player($('iframe')[0]);
}

if (!thisvid) onYouTubePlayerAPIReady();

$('button').click(function () {
    thisvid.pauseVideo();
});

jsfiddle

我无法使用 JS 创建视频 iframe ……所以我需要针对给定 iframe 的解决方案。

提前致谢!

关于这个answer,我发现我错了以下几点: 1. 如果在 iframe src 中传递了 "&origin=" -> 它不会起作用。 2. 在 js-fiddle 只有当 FWs 和 EXTs 加载到 body 中而不是加载时它才有效。

好吧,这就是我所需要的:) 这是一个有效的 fiddle.

和代码…… … HTML:

<iframe width="420" height="315" src="https://www.youtube.com/embed/2BagaJFimzk?autohide=1&showinfo=0&rel=0&enablejsapi=1&origin=http%3A%2F%2Fjsfiddle.net" frameborder="0" allowfullscreen></iframe>
<button>please stop :(</button>

...和JS(当然你必须嵌入https://www.youtube.com/iframe_api):

var thisvid;

function onYouTubePlayerAPIReady() {
    thisvid = new YT.Player($('iframe')[0]);
}

if (!thisvid) onYouTubePlayerAPIReady();

$('button').click(function () {
    thisvid.pauseVideo();
});