YouTube player api - addEventListener() does not w

2019-07-16 06:17发布

I'm using the youtube player api. I'm following the doc here:

http://code.google.com/apis/youtube/js_api_reference.html

having a problem adding an event listener on the player, it seems to just get stuck there. I put an alert statement directly after, which never gets called:

ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
alert("I never get called...");

any idea why this would happen? Running it through FF chrome safari, same thing happens on all. Other player features all work fine. I'm using the swfobject version. I'm running this live from a server too. I don't get it.

Thanks

4条回答
【Aperson】
2楼-- · 2019-07-16 06:42

I think it's a bug. Here's the solution that worked for me:

In the Player URL, you must include a '&' after the ?. So the example is this:

VIDEO_ID?enablejsapi=1&playerapiid=ytplayer

But use this instead:

VIDEO_ID?&enablejsapi=1&playerapiid=ytplayer

(note the & before 'enable')

Doesn't make much sense, but it's working now :)

查看更多
甜甜的少女心
3楼-- · 2019-07-16 06:51

take a look at the source code of this page, may be it will get you going in the right direction. The number under the video is the state of the video caught and updated the way you are trying to implement

http://goo.gl/b93ns

查看更多
地球回转人心会变
4楼-- · 2019-07-16 06:58

Try using Firebug http://getfirebug.com/ for FireFox, it should help you find any javascript errors. I'm having problems with the youtube event listeners too but mine work in Firefox.

查看更多
做个烂人
5楼-- · 2019-07-16 07:05

Place your callback in the global namespace, eg window.onytplayerStateChange = function.

While its pretty crappy its one of the few ways i was able to successfully get the callback to work. I was told its because the addEventListener is a custom function overriding the native code that does a lookup via string in the global namespace.

查看更多
登录 后发表回答