簡體   English   中英

如何在Flex應用程序中運行外部SWF?

[英]How to run an external SWF inside a Flex Application?

編輯:由於答案我更改了發布的代碼。 我添加了 Security.allowDomain("*") 行,該行會引發錯誤。 那么,怎么做呢?

我想將Action Script 3.0應用程序運行到Flex應用程序中。 為此,我做了以下事情:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication windowComplete="loadSwfApplication()" xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
            private function loadSwfApplication()
            {
                // The next line throws me an error.
                Security.allowDomain("*");

                var urlRequest:URLRequest = new URLRequest("path/to/the/application.swf");
                swfLoader.addEventListener(Event.COMPLETE, loadComplete);
                swfLoader.load(urlRequest);
            }

            private function loadComplete(completeEvent:Event)
            {
                var swfApplication:* = completeEvent.target.content;
                swfApplication.init();  // this is a Function that I made it in the Root class of swfApplication
            }
        ]]>
    </mx:Script>

    <mx:SWFLoader id="sfwLoader"/>

</mx:WindowedApplication>

問題是在調用swfApplication.init(); AIR Player拋出了一個異常:

安全沙箱沖突:調用者文件:///path/to/the/application.swf無法訪問app所擁有的階段:/SWFApplicationLoader.swf。

這是因為在application.swf某個地方我使用這樣的舞台:

if (root.stage != null)
    root.stage.addEventListener(Event.REMOVED, someFunction);
root.stage.stageFocusRect = false;

如何加載此swf應用程序並使用舞台沒有任何問題?

您可以嘗試將SWF臨時加載到ByteArray ,然后使用SWFLoader加載它。

不要忘記將allowCodeImport設置為true,因為您的SWF內部有代碼。

當然,請確保您加載的swf對您的應用程序足夠安全,因為它可以訪問您的所有屬性。

private function loadSwfApplication():void {
  // load the file with URLLoader into a bytearray
  var loader:URLLoader=new URLLoader();

  // binary format since it a SWF
  loader.dataFormat=URLLoaderDataFormat.BINARY;
  loader.addEventListener(Event.COMPLETE, onSWFLoaded);

  //load the file
  loader.load(new URLRequest("path/to/the/application.swf"));
}
private function onSWFLoaded(e:Event):void {
 // remove the event
 var loader:URLLoader=URLLoader(e.target);
 loader.removeEventListener(Event.COMPLETE, onSWFLoaded);

 // add an Application context and allow bytecode execution 
 var context:LoaderContext=new LoaderContext();
 context.allowCodeImport=true;

 // set the new context on SWFLoader
 sfwLoader.loaderContext = context;

 sfwLoader.addEventListener(Event.COMPLETE, loadComplete);

 // load the data from the bytearray
 sfwLoader.load(loader.data);
}

// your load complete function
private function loadComplete(completeEvent:Event):void {
 var swfApplication:* = completeEvent.target.content;
 swfApplication.init();  // this is a Function that I made it in the Root 
                         // class of swfApplication
}

如果從不同的域加載它們,則必須添加安全性異常 - http://livedocs.adobe.com/flex/3/html/help.html?content=05B_Security_08.html

如果它在本地運行,你可能需要將它們添加到設置管理器中的可信文件或文件夾列表中 - http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html# 117502

假設外部SWF也在應用程序目錄中,您可以嘗試使用app:/ scheme加載它:

var urlRequest:URLRequest = new URLRequest("app:/path/application.swf");

這可能會將其置於與主應用程序相同的安全上下文中。

您可能想要考慮的一件事是,如果您嘗試從AIR中的應用程序目錄中運行SWF,則AIR會限制文件的執行。 如果將文件復制到臨時文件並運行它(以及allowLoadBytesCodeExecution設置為true ),那么它可以正常工作。

var file:File = File.applicationDirectory.resolvePath("myFile.swf");
this.tmpFile = File.createTempDirectory().resolvePath("myFile.swf");
file.copyTo(this.tmpFile);

imagePreview.loaderContext = lc;
imagePreview.source = tmpFile.url;

它不適用於Flex投影儀。

只有我們使用SWFLoader和LocalConnection,因為它們可以在外部swf和主swf之間進行通信。 感謝你的支持!

你能從Adobe論壇上閱讀我的教程嗎?

它比MovieClip或Object-Callers更好

感謝解決方案:)

此致,Jens

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM