2010-07-20 102 views
0

我即将为Flash开发人员编写一个规范来构建一个类似于HTML5 <audio>标记的播放器控件。Flash ExternalInterface和函数回调问题

其中一个关键点是如何添加回调,例如,你会做类似的音频标签:

getElementById('flashAudioPlayer').addEventListener('timeupdate', function() { 
    // Do stuff 
}); 

是否有可能有Flash控件支持这种类型的回调传球?而不是静态的'ExternalInterface'调用,它会调用传入的函数对象,就像上面的例子。

谢谢!

回答

2

也许这是可行的使用JS eval从Actionscript方面,但我认为有一个副本。

让你的swf调用一个JS代理,然后将消息广播给注册的监听器。

东西沿着这些路线:

JS

var proxyDispatcher = new ProxyDispatcher(); 
var app = getYourSwf("myswf"); 
// tell your app to call the dispatch method in the proxyDispatcher object everytime theres a timeupdate 
// dispatch will receive the name of the event back, plus the "payload" data (that's up to you to define) 
app.registerCallback('proxyDispatcher.dispatch','timeupdate'); 
// now, add "clients" here 
// the idea is that proxyDispatcher registers JS functions to be called when its dispatch method is called 
// so, when dispatch is called with 'timeupdate' as the event type, call all listeners for 'timeupdate'... 
proxyDispatcher.addEventListener('timeupdate',function() { 

}); 

AS

var _map:Object = {}; 

private function handleRegisterCallback(jsCallback:String,eventName:String):void { 
    _map[eventName] = jsCallback; 
} 

    // when there's a timeupdate, check if you have a registered listener for it; if so, call it 
private function dispatchTimeupdate():void { 
    if(_map['timeupdate']) { 
     // call the registered function, pass the event name and any data you need 
     ExternalInterface.call(_map['timeupdate'],'timeupdate','your data here'); 
    } 
} 

这可以简化,如果你硬编码更多的东西,但也许这种方法并不复杂,以实现并给出你有一定的灵活性,并使注册听众容易JS“客户端”(涉及更多的部分将在ProxyDispatcher本身)。

希望这是有道理的!

0

当然,您仍将使用ExternalInterface实现解决方案,但没有理由不能告诉Flash哪些函数可以调用。 ExternalInterface只是使用字符串来调用JS函数,所以你可以将它们传递给Flash应用程序来告诉它你希望它使用哪一个。

您可以将它们作为Flash变量或ExternalInterface传递。