AIR app:如何从服务器加载swf

时间:2015-08-20 14:10:26

标签: actionscript-3 flash flex air

我的应用结构如下:

(local)
Login.swf
(server)
Main.swf
assets1.swf
assets2.swf

Login.swf -> Main.swf (OK!)
Main.swf -> assets1&2.swf (fail!, downloaded but not trigger complete event)
-progress event: bytes:loaded==total

为什么?
如何使用Main.swf从服务器加载资源?
我发现有人说这是跨域问题...然后如何解决?

var _loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.allowCodeImport = true;

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete_handler);
_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loader_progress_handler);
_loader.loadBytes(_urlloader.data,context);

谢谢!

1 个答案:

答案 0 :(得分:0)

BE AWARE:

在移动设备上它只适用于Android ... iOS由于安全策略不允许在另一个内部加载swf。

说你可以确定加载一个SWF。

首先,我们需要了解您是否可以使用URLLoader下载SWF bytearray。

如果是,你可以在两步程序中加载swf ..

希望这有帮助:)

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx"               
                       showStatusBar="false"
                       creationComplete="downloadSwf()">

    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
            import mx.events.CloseEvent;
            import mx.events.FlexEvent;
            import mx.managers.SystemManager;

            import spark.components.Application;

            private function downloadSwf():void {
                // load the file with URLLoader into a bytearray
                sfwLoader.visible = false;                                                      

                var loader:URLLoader = new URLLoader();

                // binary format since it a SWF
                loader.dataFormat=URLLoaderDataFormat.BINARY;
                loader.addEventListener(Event.COMPLETE, onSWFLoaded);
                loader.addEventListener(IOErrorEvent.IO_ERROR, cantConnect);
                //load the file             
                loader.load(new URLRequest("url"));
            }

            private function cantConnect(e:IOErrorEvent):void
            {               
                var loader:URLLoader=URLLoader(e.target);
                loader.removeEventListener(Event.COMPLETE, onSWFLoaded);
                loader.removeEventListener(IOErrorEvent.IO_ERROR, cantConnect);
                // error handling   
                return;
            }

            private function onSWFLoaded(e:Event):void {
                // remove the event
                var loader:URLLoader=URLLoader(e.target);
                loader.removeEventListener(Event.COMPLETE, onSWFLoaded);
                loader.removeEventListener(IOErrorEvent.IO_ERROR, cantConnect);

                // 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 {
                sfwLoader.removeEventListener(Event.COMPLETE, loadComplete);

                var swfApplication:* = completeEvent.target.content;

                var sysmgr:SystemManager = (swfApplication as SystemManager);
                sysmgr.addEventListener(FlexEvent.APPLICATION_COMPLETE, swfAppComplete); 

            }

            private function swfAppComplete(event:FlexEvent):void {
                sfwLoader.visible = true;
                var sysmgr:SystemManager = (event.currentTarget as SystemManager);
                sysmgr.removeEventListener(FlexEvent.APPLICATION_COMPLETE, swfAppComplete);
                var swfApp:Application = (sysmgr.application as Application);               
                if (!swfApp.hasOwnProperty("version")) {
                    return;                 
                }
                // version is a public property in the main of the loaded application
                var verion:String = swfApp["version"];          
                swfApp.addEventListener(Event.CLOSE, appClose);
            }

            private function appClose(e:Event):void
            {
                sfwLoader.unloadAndStop(true);
                NativeApplication.nativeApplication.exit();
            }

            private function closeApp(e:CloseEvent):void
            {
                try{
                    (FlexGlobals.topLevelApplication as WindowedApplication).close();   
                }
                catch(e:Error){}                
            }           
        ]]>
    </fx:Script>    
    <mx:SWFLoader id="sfwLoader" 
                  width="100%" visible="false"
                  height="100%"/>
</s:WindowedApplication>