我有youtube播放器OnStateChange不开火的问题。我看过其他的帖子,他们不太适合我的情况,因为我可能使用不同的(无色javascript)版本。
HTML:<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
Javascript: 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");
}
我基本上复制了文档中的示例。然后输入
var player = document.getElementById('player');
我可以让它正常工作。但是唯一的问题是当它改变状态时,OnStateChange事件不会触发。代码如下:
document.getElementById('player').addEventListener('OnStateChange',
function(new_state){
console.log(new_state);
}
)
新状态从未被打印出来。我做错了吗?http://jsfiddle.net/8QtrC/1/
这个例子适合我。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<body>
<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<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);
}
</script>
</body>