$(document).ready(function ()
{
var player_1_listener = $('#myVid_1')[0];
$f(player_1_listener).addEvent('ready', ready);
var player_2_listener = $('#myVid_2')[0];
$f(player_2_listener).addEvent('ready', ready);
function addEvent(element, eventName, callback)
{
if (element.addEventListener)
{
element.addEventListener(eventName, callback, false);
}
else
{
element.attachEvent(eventName, callback, false);
}
}
function ready(player_id)
{
//var froogaloop = $f(player_id);
if (player_id === myVid_1)
{
var froogaloop = $f(player_id);
function onFinish()
{
froogaloop.addEvent('finish', function(data)
{
/*var players_unloading;
players_unloading=$('#myVid_1');
$f(players_unloading[0]).api('unload');*/
toggleOverlay_1();
$f(froogaloop[0]).api('unload');
});
}
//onFinish();
}
else if (player_id === myVid_2)
{
var froogaloop = $f(player_id);
function onFinish()
{
froogaloop.addEvent('finish', function(data)
{
/*var players_unloading;
players_unloading=$('#myVid_1');
$f(players_unloading[0]).api('unload');*/
toggleOverlay_2();
$f(froogaloop[0]).api('unload');
});
}
//onFinish();
}
}
onFinish();
});
I have 2 buttons which on clicking open an iframe and play videos. I am trying to get the player within the iframe to call it's api pause method to pause the video. I can do this well if I use only one iframe video, but I am unable to do when there are multiple iframe videos.
You will need to work with adding parameters to the functions in a way to re-use as much code as possible. The way i see it you can add playerid in all related functions and use that to minimize code within the
ready(player_id)
by utilizing theplayer_id
as main id to help access all related video items.Example using froogaloop2.js lib[old]: http://codepen.io/Nasir_T/pen/pEmEJE
Example using player.js from vimeo[new improved]: http://codepen.io/Nasir_T/pen/GNKjbe
Let me know if you need more help.