My insert code for playing Youtube video:
<object height="356" width="425" type="application/x-shockwave-flash" id="myytplayer" data="http://www.youtube.com/v/MTf6qXn5Prw?enablejsapi=1&playerapiid=ytplayer&version=3"><param name="allowScriptAccess" value="always"></object>
I want to track Youtube Player events (play / pause / stop, etc.)
The following code snippet works independently
window.onYouTubePlayerReady = function(playerId){ ytplayer = document.getElementById("myytplayer"); ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); } window.onytplayerStateChange = function (newState) { alert("Player new state: " + newState); }
I am using YUI.
When I put the same in
YUI.add('module-name', function(Y) { [some other code...] window.onYouTubePlayerReady = function(playerId){ // console.log(playerId); console.log(ytplayer); ytplayer = document.getElementById("myytplayer"); ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); } window.onytplayerStateChange = function (newState) { alert("Player new state: " + newState); } },'3.4.0', {requires:'module-a', 'module-b'})
The onytplayerStateChange function works in Firefox and Safari, but not in other browsers.
Then I tried the YUI functions to make it work in all browsers, so I made some changes
window.onYouTubePlayerReady = function(playerId){ var shinyPlayer = Y.one("#myytplayer"); shinyPlayer.on('onStateChange', function (e) { e.preventDefault(); alert('here'); }); }
but it did not work for me.
I do not want to place window.onytplayerStateChange outside of YUI.add ('module-name', function (Y) {})
Please suggest what to do to track the status of the Youtube player in all browsers.
Thanks in advance.