我遇到了 YouTube 播放器 OnStateChange 未触发的问题。我看过其他帖子,它们不太适合我的情况,因为我可能使用不同的(chromeless 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>
关于javascript YoutubePlayer OnStateChange 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24299693/