Loading Swf with bytearray in AIR

airapache-flexflash

We have requirement with the AIR application which loads the flex generated swf which inturn loads the flash generated swf using SWFLoader. This is not working as desired. This gives the following error:
SecurityError: Error #3226: Cannot import a SWF file when LoaderContext.allowCodeImport is false.

This is our AIR application.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[

import mx.controls.SWFLoader;

[Embed(source="FlexLoadingFlash.swf")]
public var flexMovie:Class;

private function initApp():void {
    // First convert the Swf into MovieClip
    var movieclip:MovieClip = new flexMovie();

    // get the byteArray from movieClip
    var byteArray:ByteArray = movieclip.movieClipData; 

    var swfLoader:SWFLoader = new SWFLoader();

    // load bytearray into swfLoader
    swfLoader.source = byteArray;

    swfLoader.maintainAspectRatio = false; 
    swfLoader.percentHeight = vbox.height;
    swfLoader.percentWidth = vbox.width;
    swfLoader.invalidateDisplayList();
    swfLoader.invalidateSize();

    // now add the swfloader into container
    vbox.addChild(swfLoader); 
} 

]]>

</mx:Script>

<mx:VBox id="vbox" width="100%" height="100%" verticalCenter="0" horizontalCenter="0" cacheAsBitmap="true" >

</mx:VBox>

</mx:WindowedApplication>

Please let me know how can we fix this issue.

Best Answer

Use Loader.loadBytes() to load your SWF. Create an instance of LoaderContext. loadBytes method takes a LoaderContext instance as a parameter. Set the allowCodeImport property of your LoaderContext instance to true and it should work

Related Topic