1
votes

I'm trying to load an embedded swf (which acts as a sound library). Since it's a large swf I want to load it at runtime and have a loaderbar indicate how much is loaded.

Doing some research online I found that using mimeType="application/octet-stream" would be the way to do this. The result however is that the embedded swf is still loaded at the same time the main/wrapper swf is loading. So for some reason the entire embedded swf is already loaded when I get to the first line of code (in this case the LoadTest constructor). Most examples I found deal with Flex and I'm using a 100% as3 project (in FlashDevelop) but I see no reason why this should work any different.

Here is the class I'm using (the real class is much more complex, but this would be the basic mechanics of loading the embedded swf)

package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.system.LoaderContext;

public class LoadTest extends Sprite {

    [Embed(source = "soundLib.swf", mimeType="application/octet-stream")]
    private var _soundLibSWFClass:Class;
    private var _assetLdr:Loader;

    public function LoadTest():void {
        this._assetLdr = new Loader();
        this._assetLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, this.handleComplete);
        this._assetLdr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.handleProgress);
        this._assetLdr.loadBytes(new _soundLibSWFClass(), new LoaderContext(false, this._assetLdr.contentLoaderInfo.applicationDomain));

        trace("start");
    }
    public function handleComplete(event:Event):void {
        trace("complete");
    }
    public function handleProgress(event:ProgressEvent):void {
        trace("progress " + event.bytesLoaded +", "+ event.bytesTotal);
    }

}
}

Here is what I get in my output:

progress 0, 2411787
progress 2411787, 2411787
start
complete

Here is some more info on the embedded swf and the stuff I tried so far:

  • The sound library is created in Flash cs5 and all sounds are located in the library and have proper identifiers.
  • The stage of the embedded swf is empty.
  • With the "Export classes in frame" option I tired both 1 and 2 but that made no difference.
  • I have the same problem with loading other swf libraries (containing movie clips for example) but the sound library seemed the most straight froward to use in the test
  • I tested this on different servers
  • I cleared the cache before each test.
  • I used a bandwith-simulation to be 100% sure that the entire thing would take a while to load.
  • Even if I remove all the loader stuff the main swf would still take some time to load which indecate that the embedded swf is loaded at the same time.

Hope anyone can help me with this issue or has another approach to load embedded swf at runtime (basicly I need a single swf without the entire thing being loaded at once).

Thanks!

1
Thank you sir for showing me how to set the LoaderContext of an embedded SWF :DJono

1 Answers

2
votes

The Embed-tag is used to bake the data into the swf at compile time. What you want to use is the .load -function on the Loader-object and remove the Embed-tag altogether.

this._assetLdr.load(new URLRequest("soundLib.swf"));

Of course, you need to also make sure that the soundLib.swf is located in the same folder as your compiled swf.