5

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.

Community
  • 1
  • 1
wao813
  • 468
  • 1
  • 5
  • 14
  • Can you put this in a jsfiddle? I'm not familiar with the youtube API but this looks like it can be fixed by changing your scoping. – devnill Dec 13 '13 at 21:18
  • 1
    maybe i misunderstand the question but it sounds like you need multiple event listeners for each uniqe player instead of having only one. –  Jan 23 '14 at 13:42

1 Answers1

1

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) 
Community
  • 1
  • 1
Mario Levrero
  • 3,345
  • 4
  • 26
  • 57