| How to Preload Sound Objects that use the attachSound Method | Return to Index | Do you want all FLA's used for this tutorial? | Do you have a question? |
The main reason to use attachSound versus loadSound is that you need the sound to be available instantly - regardless of its size (in megabytes). If you can put up with a sound loading for a few seconds when its called into action, you should use loadSound set to streaming (in general). See the section on loadSound for more information.
When using the attachSound method for sound objects (which embeds the sound in your SWF file), one challenge that must be considered is the load time for the sound objects. Sound objects that use the attachSound method must export on the first frame. This means that if you have a preloader that starts on frame one also, the sound objects will load before your preloader code. Obviously, if you have 1.5 MB of sound objects in your file, there will be an awkward delay prior to the loading of your preloader. Frankly, if your sounds are more than 25 KB in total, I would preload as described here. The following outlines a solution to this problem, and is much easier than it may sound initially.
The solution is to create your sound objects (that use use the attachSound method) in a separate FLA file, and import the resulting SWF into your movie using the loadMovie command. Following these steps, you will still have access to all of the properties and methods of the sound object (position, volume level, panning, etc.) that make it so appealing.
Start a new FLA, the purpose, of which, will only be to create your sound object(s) embedded into an SWF file. Create your sound objects as previously described, with one important difference. You must include the keyword "this" as you define the object (as shown below).
_root.firstSound=new Sound(this); _root.firstSound.attachSound("firstSound01"); _root.secondSound=new Sound(this); _root.secondSound.attachSound("secondSound01"); |
Notice that I've defined the sound objects as existing on the _root level. This will allow you to refer to them as _root.firstSound and _root.secondSound, even if they are loaded into another movie later.
Import your sounds into the library of this FLA. Make sure the linkage properties are properly configured (as described in figure 2). Besides the linkage IDs, the above code is the only ActionScript that is needed in this FLA. Publish the FLA to create your SWF, which now contains your sound objects. Note that you do not need to include a command to start the sound playing in this file. For this example, the SWF that contains the sound objects will be called "externalSoundObjects.swf".
The FLA that is used to create the SWF containing the sound objects should be small enough (dimension-wise) that when it loads into the main FLA, it will not been seen (unless you want it to be seen). If you placed the empty movie clip that that the external SWF will load into - off stage, this will not be an issue. For example, set the document dimensions to be 5 pixels by 5 pixels.
Inside the movie that will load the file "externalSoundObjects.swf", create an empty movie clip and place it anywhere (even off stage). Name the empty movie clip whatever you want. For this example, the movie clip will have the instance name of "musicHolder", and it will be placed on the _root timeline just off stage.
Alternatively, you could use loadMovieNum() and load the sound objects (externalSoundObjects.swf) into a level of the main movie. However, I prefer to load them into an actual empty movie clip with an actual instance name.
This second movie clip, with the instance name of "loopMc", will contain the clip events for loading the sound and tracking the bytes loaded. You cannot place the ActionScript on the musicHolder movie clip, because all ActionScript on the musicHolder movie clip will be replaced with the contents of the external file that will load into the empty movie clip.
Note: You do not need to redefine the sound objects in the main FLA that will load the external SWF file.
To load the sound objects, paste the following ActionScript into the Actions pane of the second empty movie clip called "loopMc":
|
onClipEvent (load) { |
Make sure that the externalSoundObjects.swf file is located in the same folder as the main SWF. Otherwise, change the script to include the folder name or absolute URL to the external file to be loaded.
In the above script, the code for running the preloader is not allowed to run unless _root.loadSound == 1. This is only needed if you want to preload the sound at a time other than initial load of the main movie. For example, if you wanted to load the sound from a button, you would use the following:
| on(release) { _root.soundHolder.loadMovie("externalSoundObjects.swf"); _root.loadSound=1; } |
If you use the script this way, do not forget to remove "_root.loadSound=1;" from the onClipEvent(load).
Note: If you get the error message, "Clip events are permitted only for movie clip instances", then you have most likely placed the script on a keyframe instead of on the movie clip.
To access the properties or methods of the sound object, specify the path to the empty movie clip that is serving as the container, and the name of the sound object. The following are some sample ways that the above sound objects would be used:
mySoundPosition=_root.firstSound.position; or firstSoundVolume=75; |
Here is an example FLA.
Fig. 23: Example of preloading attachSound sound objects as an external SWF |
This document copyright © 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com