相同的SWF的不同实例之间的LocalConnection?实例、不同、SWF、LocalConnection

由网友(萌面大瞎)分享简介:我要建一个简单的音频播放器在AS3中。我想这个嵌入的音频播放器在我的HTML,当一个人打,其他人被暂停的多个实例。是否有可能使用LocalConnection相同的swf的不同实例之间的沟通?从我能测试,一旦连接是用一个实例,别人抛出一个错误... I'm building a simple audio player...

我要建一个简单的音频播放器在AS3中。我想这个嵌入的音频播放器在我的HTML,当一个人打,其他人被暂停的多个实例。是否有可能使用LocalConnection相同的swf的不同实例之间的沟通?从我能测试,一旦连接是用一个实例,别人抛出一个错误...

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...

我的另一种选择是使用JavaScript。任何其他的想法?

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

推荐答案

对于大家感兴趣的话,这里是我的简单实现。

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);
阅读全文

相关推荐

最新文章