How to run external SWF inside Flex application?

EDIT: Due to the answer, I am changing the code. I added a line Security.allowDomain("*") , and this line causes an error. So how can this be done?

I want to run an Action Script 3.0 application in a Flex application. To do this, I did the following:

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

The problem is that when calling swfApplication.init();AIR Player, it throws an exception:

sandbox security violation: caller file: ///path/to/the/application.swf cannot access the scene belonging to the application: /SWFApplicationLoader.swf.

This is because somewhere in application.swfI use the scene as follows:

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

How can I download this swf application and USE the scene without any problems?

+5
5

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
}
+18

, SWF , , app:/:

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

, .

+1

, SWF AIR, AIR . temp ( 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;
0
source

It will not work for Flex projectors.

Only we use SWFLoader and LocalConnection because they can communicate between external swf and main swf. Thanks for the support!

You can read my tutorial from the Adobe Forum.

This is much better than MovieClip or Object-Callers.

Thanks for the resolved decision :)

Best regards, Jens

0
source

All Articles