1

How to stop the (youtube) video on click? I've tried so much, but I can't get it working … what am I doing wrong? Regarding to the docs and hundreds of posts in the internet this should work like a charm … but it does not:

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>

in js (with jquery and youtube-api loaded):

var thisvid;

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

if (!thisvid) onYouTubePlayerAPIReady();

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

on jsfiddle

I can't create the video-iframe with JS … so I need a solution for a given iframe.

THX in advance!

John Doe Smith
  • 1,623
  • 4
  • 24
  • 39

1 Answers1

2

Regarding to this answer, I found out that I made the following points wrong: 1. If "&origin=" is passed in iframe src -> it won't work. 2. at js-fiddle it only works if FWs & EXTs are loaded in body and not on load.

Well, that's all I need :) Here is a working fiddle.

and the code … … 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>

… and the JS (of course you have to embed https://www.youtube.com/iframe_api):

var thisvid;

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

if (!thisvid) onYouTubePlayerAPIReady();

$('button').click(function () {
    thisvid.pauseVideo();
});
Community
  • 1
  • 1
John Doe Smith
  • 1,623
  • 4
  • 24
  • 39