如何在Flex应用程序中运行外部SWF?

问题描述:

编辑:由于答案我更改发布的代码。我已经添加了Security.allowDomain("*")行,该行会引发错误。那么,怎么做呢?如何在Flex应用程序中运行外部SWF?

我想在Flex应用程序中运行一个Action Script 3.0应用程序。要做到这一点,我已经做了以下内容:

<?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播放器的调用抛出了我的异常:

安全性违规:呼叫者文件:///路径/to/the/application.swf无法访问由应用程序拥有的Stage:/SWFApplicationLoader.swf。

这是因为某处application.swf我用这样的阶段:

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

我如何可以加载这个SWF应用程序和使用阶段,没有任何问题?

您可以尝试将您的SWF暂时加载到ByteArray中,然后使用您的SWFLoader加载它。

不要忘记将allowLoadBytesCodeExecution设置为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.allowLoadBytesCodeExecution=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 
} 
+0

这也适用于移动平台吗?如果是的话,你可以写一个适用于iOS的Flash播放器。 – sydd 2011-09-03 22:44:57

+0

任何想法,为什么这与字节数组,但不swfloader组件的作品? – buddyp450 2013-03-20 07:56:53

如果他们正在从你将不得不增加一个安全异常不同的域加载 - http://livedocs.adobe.com/flex/3/html/help.html?content=05B_Security_08.html

如果正在运行本地的youre可能将不得不把它们添加到信任的文件或文件夹列表设置管理器 - http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html#117502

+0

当我尝试添加安全异常它会引发一个错误: 'SecurityError:错误#3207:应用程序 - 沙箱内容无法访问此功能。 \t at flash.system :: Security $/allowDomain()' – 2010-04-29 18:56:29

+0

它已被添加到设置管理器中的可信文件中。 – 2010-04-29 18:57:25

假设外部SWF也是在应用程序目录,你可以尝试使用app:/方案加载它:

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's Forum

我的教程它比影片剪辑或对象的来电者非常好

感谢解析解:)

最好的问候,延