关于youtube api:javascript YoutubePlayer OnStateChange不触发

javascript YoutubePlayer OnStateChange not firing

我对youtube播放器OnStateChange无法启动有问题。我看过其他帖子,但它们与我的情况不太吻合,因为我可能正在使用其他(无镶边javascript)版本。

HTML:

1
 You need Flash player 8+ and JavaScript enabled to view this video.

Javascript:

1
2
3
4
5
6
7
8
var params = { allowScriptAccess:"always", wmode:"Opaque" };
var atts = { id:"player" };
swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&version=3",
                  "ytapiplayer","100%","100%","8", null, null, params, atts);

function onYouTubePlayerReady(playerId){
  player = document.getElementById("player");
}

我基本上将示例复制到文档中。然后我做

1
var player = document.getElementById('player');

我可以使它正常工作。但是唯一的问题是,当它更改状态时,不会触发OnStateChange事件。代码像这样。

1
2
3
4
5
document.getElementById('player').addEventListener('OnStateChange',
  function(new_state){
     console.log(new_state);
  }
)

新状态永远不会被打印出来。我做错了吗? http://jsfiddle.net/8QtrC/1/


这个例子对我有用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js">

<body>

 
    You need Flash player 8+ and JavaScript enabled to view this video.
 

<script type="text/javascript">

    var params = { allowScriptAccess:"always" };
    var atts = { id:"myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/Bl4Qne-RpcM?enablejsapi=1&playerapiid=myytplayer&version=3",
                      "ytapiplayer","425","356","8", null, null, params, atts);

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
  ytplayer.addEventListener("onStateChange","onytplayerStateChange");
}

function onytplayerStateChange(newState) {
   alert("Player's new state:" + newState);
}
 
</body>