YouTube Iframe-API: getting the iframe-id inside 'onStateChange' function -
i using youtube javascript api control youtube players on page. goal stop playing videos, when start new one.
already going through struggle of different kinds of youtube embed options, single videos, playlist etc. came question, make life more easier.
initializing players this:
// array store ytplayers var ytplayers = []; // select youtube iframes var ytembeds = $('iframe[src*="youtube.com"], iframe[src*="youtube-nocookie.com"]'); ytembeds.each(function(i) { iframe = $(this); // generate , add unique identifier current iframe var iframe_id = "iframe-youtube-" + i; iframe .attr("id", iframe_id); // check if src has "?" , add "?" or "&" accordingly // add necessary "enablejsapi=1" option , update src. var url = iframe .attr("src"); url += url.indexof("?") == -1 ? "?" : "&"; iframe.attr("src", url + "enablejsapi=1"); // create youtube player object custom eventlistener player = new yt.player( iframe_id, { events: { 'onstatechange': onyoutubeplayerstatechange } }); // store new player element , iframe_id ytplayers.push({ "id" : iframe_id, "player" : player }); });
now inside function "onyoutubeplayerstatechange", there have access "event"-object, there information embedded youtube-player , nothing ... can find ... surrounding iframe-object.
so question is: how can access iframe-id, got used initialize yt-player, inside function "onyoutubeplayerstatechange"?
ps: have used video_id, or playlist_id, stored in event-object identify iframe. when have 2 players same yt-video_id, although case unlikely, both keep running.
thanks effort me problem.
kindly,
max k
so found pretty simple solution, pass argument eventlistener function wrapping in function, , passing default "event" object plus customly defined iframe_id, so:
player = new yt.player( iframe_id, { events: { 'onstatechange': function(event){ onyoutubeplayerstatechange(event, iframe_id); } } });
now, in "onyoutubeplayerstatechange" function have access both youtube-event-object current state of player , iframe_id defines player is.
cheers!
Comments
Post a Comment