我的javascript:
function onYouTubePlayerReady()
{
playerObj = document.getElementById("player_embed");
playerObj.addEventListener("onStateChange", test);
// Make sure the document is loaded before any DOM-manipulation
$(document).ready(function() {
// Update player information every 500ms
setInterval(updatePlayerInfo, 500);
});
}
function test(newState)
{
alert(newState);
}
我的视频已正确加载,我可以通过我的javascript控制它们。但是onStateChange事件似乎永远不会触发。当我正在播放/停止/暂停视频等时,它从未提供警报。任何有解决方案/类似问题的人?
答案 0 :(得分:4)
您必须将函数名称作为字符串传递给addEventListener
。
playerObj.addEventListener("onStateChange", "test");
答案 1 :(得分:0)
这种格式适用于JQuery,但逻辑相同,在onYouTubePlayerReady中创建一个函数来传递playerId
$(document).ready(function() {
onYouTubePlayerReady = function(playerId) {
eventHandler=function(state){onStateChangeID(state,playerId);}
playerObj = document.getElementById("player_embed");
playerObj.addEventListener('onStateChange','eventHandler',false);
}
onStateChangeID=function(newState,playerId) {
console.log("onStateChange: ("+playerId+") " + newState);
}
});