Coswell Productions

Flex 3: Better SWF Embedding

For a recent Flex project I have to work with a series of SWF files that need to be embedded into the output SWF. I also needed to be able to control the SWF files and be notified when they finish playing. Unfortunately, when you embed a SWF in the standard way:

[Embed(source="my_swf_file.swf")]
[Bindable]
public static var my_swf_file:Class;

and then try to work with it as a MovieClip:

var my_clip:MovieClip = new my_swf_file() as MovieClip;

all of the Timeline bits (including totalFrames and currentFrame) of the source SWF get stripped out in the embedding. To fix it, I took the idea from this blog entry and made it a bit more generic:

[Embed(source="my_swf_file.swf", mimeType="application/octet-stream")]
[Bindable]
public static var my_swf_file:Class;
var source:Object = new my_swf_file();
var my_clip:MovieClip;
if (source is ByteArray) {
  var loader:Loader = new Loader();
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event):void) {
    my_clip = MovieClip(loader.content);
  });
}

Et voila! Embedded, controllable SWF files in Flex.

See what else is here...