LocalConnection between different instances of the same SWF?

I am building a simple audio player in AS3. I would like to embed multiple instances of this audio player in my html, and when you play, other pauses. Can LocalConnection be used to communicate between different instances of the same SWF? From what I could check, once the connection is made to one instance, the others throw an error ...

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

+2


source to share


2 answers


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);

      

+4


source


This works well in IE, FF.

in chrome return document[movieName];

returns undefined.



i is just replaced with return document.getElementById(movieName)

. now also works Chrome.

+1


source







All Articles