Flash: Preloading a file with embedded sound

Jul 26, 11:04 AM |

Problem: You have a large sound embedded inside your fla document in Flash IDE, and also the preloader. Everything works fine when offline, but when you place the swf on the web, the preloader shows up only when almost everything is loaded.

It just don’t show the process of loading the sound. (There is a blank page for a while, then the preloader shows say 98%, and then the file is loaded, making the preloader useless.)

The preferred solution is placing the sound (or all souds) outside the document as an external file. But it’s not allways possible. Sometimes you just need to have everything in one single file. Typical situation is making a game for a game portal. In most cases they don’t accept a game using multiple files. So you have to embed your music (which is several times larger than the rest) inside the fla file.

I didn’t find the solution in Flash help, and solutions provided by several people over the web didn’t work for me. So here is my solution for people who are struggling with it, and it will work both in as2 and as3.

  1. Go to the library for your sound file, and right click its properties. Leave the linkage as Export for actionScript, but NOT to export on the 1st frame.
  2. Now we need to show the Flash that we actually use the sound.
    a) Somewhere on the timeline, after your preloader, make a single keyframe. Place there your sound (in the frame properties). Set the Sync as “Start” and the Effect as “Fade in”. (We don’t want the sound to start in fact, so when it’s on one frame only and with the fade in effect, you’ll not hear anything when playing the swf).
    b) On the following frame in the same layer, create another single keyframe. Place the same sound inside, but now set the Sync as “Stop”.
  3. In as2, the preloader should now work.
    In as3 it depends on your preloader. The preloader code should be on your main timeline and the frame code should look like this:

import flash.events.ProgressEvent;
import flash.events.Event;

function onLoading(evt:ProgressEvent):void { var statusTxt:String; var loaded:Number = evt.bytesLoaded / evt.bytesTotal; statusTxt = (loaded*100).toFixed(0) + “% loaded”; //…here somehow show the status on your preloader movieClip

function onComplete(event:Event):void { this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading); this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete); play();

this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);


All done! Place the file on the web and play. The preloader should work online.





Web design (c) ThunderBird Animations, 7/2011. Powered by Textpattern.