Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the source or id from youtube player onstatechange callback

This is a question similar to how to pass arguments to addeventlistner But the scenario is a bit different by using Youtube player's api.

So I have multiple youtube player on the same page, using 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);

Where I'm using ruby to generate the ytplayer object id. And I'm listening the event onStateChange in another function.

ytplayer.addEventListener("onStateChange", "onytplayerStateChanged");

function onytplayerStateChanged(newState) {
    if(newState == -1){
        //unstarted
    }else{
        debugger;
    }
}

But the problem is I cannot know which ytPlayer this event comes from. (find the source of the caller in the onytplayerStateChanged function) Since I'm only able to catch this event by following the exact implementation of this structure. I tried the implementation on This one but it won't catch the event anymore.

like image 462
wao813 Avatar asked Nov 12 '22 23:11

wao813


1 Answers

I think you can embed an Id and then add the listener to a dynamic function as is propose here: 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) 
like image 135
Mario Levrero Avatar answered Nov 14 '22 22:11

Mario Levrero