Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  112] [ 4]  / answers: 1 / hits: 22510  / 13 Years ago, tue, february 7, 2012, 12:00:00

I want to run a function called stopSlide whenever someone clicks the 'play' button on an embedded youtube video. Or even getting the function to run when they click anywhere inside the embedded video would work fine for me right now.



How can I do this?


More From » jquery

 Answers
11

There is a YouTube JavaScript API that provides event callbacks.



There is, unfortunately, no way to directly detect a click event (at least I am not aware of any). You can, however, detect changes in the player state for which you can use onStateChange.



First, you will need to enable the JS API in the player by embedding it by using a special URL:



http://www.youtube.com/v/VIDEO_ID?version=3&enablejsapi=1


Then you need to create an event handler function:



function player_state_changed(state) {
/* This event is fired whenever the player's state changes.
Possible values are:
- unstarted (-1)
- ended (0)
- playing (1)
- paused (2)
- buffering (3)
- video cued (5).
When the SWF is first loaded it will broadcast an unstarted (-1) event.
When the video is cued and ready to play it will broadcast a video cued event (5).
*/

if (state == 1 || state == 2) {
alert('the play button *might* have been clicked');
}

}


Lastly, you need to make sure that the handler gets called whenever the state of the movie changes:



document.getElementById('MY-PLAYER-ID').addEventListener('onStateChange', 'player_state_changed');

[#87597] Monday, February 6, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
adriannemyiag

Total Points: 504
Total Questions: 105
Total Answers: 99

Location: Ecuador
Member since Thu, Apr 22, 2021
3 Years ago
;