这是一个类似于how to pass arguments to addeventlistner的问题 但是通过使用Youtube播放器的api,场景有点不同。
所以我在同一页面上有多个youtube播放器,使用swfobject:
swfobject.embedSWF("http://www.youtube.com/v/"+video_id+"?enablejsapi=1&version=3&modestbranding=1&theme=light&color=white&autohide=1&controls=1&showinfo=0&iv_load_policy=3&autoplay=0&playerapiid=<%= "ytPlayer#{index}" %>", "<%= "ytPlayer#{index}" %>", "500", "280", "8", null, null, params);
我正在使用ruby生成ytplayer对象id。 我正在另一个函数中听取onStateChange事件。
ytplayer.addEventListener("onStateChange", "onytplayerStateChanged");
function onytplayerStateChanged(newState) {
if(newState == -1){
//unstarted
}else{
debugger;
}
}
但问题是我无法知道这个事件来自哪个ytPlayer。 (在onytplayerStateChanged函数中找到调用者的来源) 因为我只能通过遵循这个结构的确切实现来捕获这个事件。我尝试了This one上的实现,但它不会再发现事件。
答案 0 :(得分:1)
我认为你可以嵌入一个Id,然后将监听器添加到动态函数中,如下所示: How to display multiple YouTube videos without overlapping audio
window["dynamicYouTubeEventHandler" + embedid] = function(state) { onytplayerStateChange(state, embedid); }
ytplayer.addEventListener("onStateChange", "dynamicYouTubeEventHandler"+embedid);
(...)
function onytplayerStateChange(newState, playerId)