ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Creating preoloaders for the attachSound Method
http://www.actionscript.org/resources/articles/88/1/Creating-preoloaders-for-the-attachSound-Method/Page1.html
Guy Watson
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. 
By Guy Watson
Published on September 9, 2005
 
Tutorial details:
Written by: FlashGuru
Difficulty Level: intermediate
Requirements: Flash 5 and higher
Download FLA

Page 1 of 2
Tutorial details:
Written by: FlashGuru
Difficulty Level: intermediate
Requirements: Flash 5 and higher
Download FLA

In flash 5 there is a new feature which seem to be causing everyone problems,this is the attachSound method which allows us to control sound with actionscript.

Why this command?!

Well many people have found that there preloaders dont work properly when using this command, the reason for this is as follows, when using the attachSound methods flash at export places all the sounds which you told to "export as symbol" in the linkage properties on a frame before the start of your movie. Basically this means that those sounds have to fully load before the rest of your movie loads. This makes is impossible to create a preoloader for your movie.

The way to overcome this is what my tutorial is about, i hope this will be very useful for many people.

Ok so to create a preloader for your sounds:

It is a good idea for many reasons to have your sound located inside of a seperate .swf file and load that movie into your main movie on the first frame. One reason for this would be continous looping background music that plays regardless of what happens in your main movie.

Ok so create a new flash movie and inside that movie on the first frame add the following script:

[as]function mysound(){
        mysoundobj=new Sound(this)
        mysoundobj.attachSound("idname")
        mysoundobj.start()
}
[/as]

Well the above script is a function that we can call from anywhere at any time and it will start to play the sound specifed by the "idname", this idname is created by choosing your sound in your library, right-click and select linkage, in the linkage properties dialog box choose "export this symbol" and give the sound an "idname".

The above function is activated by using:

[as]mysound();
[/as]

Now the movie we just created will contain all the sound for your movie, create as many functions as you want, infact you could use the one function for multiple sounds by adding a paramter for the function, something like this:

[as]function mysound(idname){
        mysoundobj=new Sound(this)
        mysoundobj.attachSound(idname)
        mysoundobj.start()
}
[/as]

that will tell the sound you send as a paramter to play, the above function is called by using:

[as]mysound("idname");
[/as]

Save the movie you have just created as "sound.fla" and publish your movie(swf only)

Page 2 of 2

Now for the preloader:

Inside of your main movie, create a new movieclip and place it on the frame that you want your sound to start loading,from your library.

Select the movieclip on the main timeline and right-click, choose actions, then copy and paste this script into the actions window:

[as]onClipEvent(load){
        loadMovieNum("sound.swf",1)
}
onClipEvent(enterFrame){
        percentloaded=Math.floor(_level1.getBytesLoaded()/_level1.getBytesTotal()*100)
        percentdisplay=percentloaded + "%"
        if(percentloaded == 100 && !initialized){
                _level1.mysound()
                initialized=true
                _visible=false
        }
}
[/as]

The above movieclip when loaded will now attempt to load the swf file named "sound.swf" into level one(_level1). The movieclip then loops everyframe(onclipEvent(enterFrame)) and checks to see if the sound movie has fully loaded, we do this by using two commands, the first is getBytesLoaded() which returns the number of bytes which have loaded for the specified movieclip and the second is getBytesTotal() which returns the total size of the specified movieclip. We then turn these values into a percentage and check to see if the percentage is equal to one hundered(100). If it is one hundred(100) then we call our function on the main timeline of level one (_level1 the level into which we loaded our movie) ashte movie is fully loaded. This function only wants to be called once when the movie is first found to be fully loaded so we use a simple boolean to determine whether we have already called the function, this is the code that says "!initialized" which basically means in english "if not initialized" then call the function, when the function is called we then set the value of initialized to be true therefore the function will not be called again from this movieclip:

[as]initialized=true;
[/as]

Now inside of this movieclip if you want to display the precentage of the movie loaded simply create a new textfield, right-click choose panels>text options and check the dynamic text box, now in the variablename field enter "percentdisplay".

If you dont want to display a petcentage then remove the line which says:

[as]percentdisplay=percentloaded + "%";[/as]

You can obviously display the number of bytes loaded etc.. by adding the correct code inside of the onClipEvent(enterFrame) parantheses.

Also inside of this movieclip you should create your loading sound animation which will play only while the sound.swf file is loading, this is easily accomplished by setting the visiblity of the movieclip to zero when the sound.swf file has loaded:

[as]_visible=false;
[/as]

So there it is the sound will now preload as and when you want it to and then play!

Some people have told me that sound doesnt play on any other level than the main movie(_level0), actually it does, the sound object (new Sound()) has an optional paramter in which you can specify a timeline, the timeline can be a movieclip, a level or _root:

[as]mysoundobj=new Sound(_root);
[/as]

if you choose to omit this parameter then the sound object is automatically created at the root of your movie(_root) but also the root of your movie on level zero(_level0) therefore i have used the "this" operator in my script above which tells the sound object to create itself on the timeline from which the script is run.

I hope this was of some use to somebody, if not then nevermind. Happy flashing peoples!