views:

375

answers:

1

Hi,

I'm building a simple audio player in AS3. I would like to embed multiple instance of this audio player in my html and when one's playing, others are paused. Is it possible to use LocalConnection to communicate between different instance of the same swf? From what I could test, once the connection is made with one instance, the others throw an error...

My other option is to use javascript. Any other ideas?

+1  A: 

For everyone interested, here's my simple implementation.

Javascript Code

//keep a reference to every player in the page
players = [];

//called by every player
function register(id)
{
    players.push(id);
}

//stop every player except the one sending the call
function stopOthers(from_id)
{
    for(var i = 0; i < players.length; i++)
    {
     var id = players[i];
     if(id != from_id)
     {
      //setEnabled is a callback defined in AS3
      thisMovie(id).setEnabled(false);
     }
    }
}

//utility function to retreive the right player
function thisMovie(movieName)
{
     if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName];
     } else {
         return document[movieName];
     }
}

AS3 Code

if(ExternalInterface.available)
{
    ExternalInterface.addCallback("setEnabled", setEnabled);
    ExternalInterface.call("register", ExternalInterface.objectID);
}

function setEnabled(value:Boolean):void
{
    //do something
}

//when I want to stop other players
if(ExternalInterface.available)
    ExternalInterface.call("stopOthers", ExternalInterface.objectID);
Subb