I have a slider which includes 4 youtube videos that are embedded via the iframe embed code
http://www.youtube.com/embed/'.$i.'?enablejsapi=1
I’m trying to make the onStateChange event of any of the four videos call a function I have called stopCycle() which will stop the slider when the video begins to play. The iframes do not have an id. I’m not sure about how to capture this event properly and could use any advice as to what i’m doing wrong.
<script charset="utf-8" type="text/javascript" src="http://www.youtube.com/player_api"></script>
var playerObj = document.getElementById("tab2"); // the container for 1 of the 4 iframes
playerObj.addEventListener("onStateChange", "stopCycle");
function stopCycle(event) {
alert('Stopped!');
}
The YouTube Frame API does support existing frames. To improve the usage, I have created some helper functions. Have a look at the code + comments below and the demo: http://jsfiddle.net/YzvXa/197
To bind functions to existent frames, you have to pass an ID reference to the frame. In your case, the frame is contained within a container with
id="tab2". I have defined a custom function for an easier implementation://Previously, core functions were defined. Look ahead for the implementation:If you want to invoke additional functions at a later point, e.g. mute a video, use:
callPlayeras defined at this answer.getFrameIDandYT_ready.