Youtube Player track recorded in YUI.add

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&amp;playerapiid=ytplayer&amp;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.

+4
source share
1 answer

I did jsFiddle to check this:

http://jsfiddle.net/2Mj6b/4/

It seems to me that addEventListener is a normal Google implementation. Usually the second parameter is a callback / pointer function and a string.

If you attach a variable to the window object, it becomes global and visible from the visible scope. So this is not a problem if you define it in the YUI module.

The second problem is that you cannot use the player as a YUI object, which must remain on dom objects in order to get this working.

Oh, this is an old question. Heck. But I will post this anyway .;)

0
source

Source: https://habr.com/ru/post/1394125/


All Articles