我在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>