2010-07-20 80 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傳遞。

相關問題