How to Use Flash MX Sound Objects

This document copyright 2006 by Kenny Bellew of Cowfly.Com Design,
Maintain Multiple, Independent Sound Object Properties Across Multiple Scenes Return to Index Do you want all FLA's used for this tutorial? Do you have a question?

Have you ever needed to have one sound object loop at a particular volume level across multiple scenes while starting and stopping other sounds that may have different volume levels? Frankly, I used to think it was impossible over multiple scenes with sound objects.

Frankly, I rarely use scenes in my Flash programming, but I know that many like to organize movies that way, and I get asked this question from time to time.

In a nut shell, this approach involves creating your sound object in its own FLA and publishing the SWF for that sound object. You will then import that SWF into your main movie (using loadMovie or loadClip) during your normal preloading events. It requires a separate SWF for each sound, so if you have 20 sounds you wish to control in this manner, you will have 20 SWF's to load. Think it through, as you may have 20 sounds but only need independent control of 5 of them.

This will allow you to maintain control of all the methods and properties that sound objects provide such as position, duration, onSoundComplete, etc.

Below is a sample of what this tutorial will produce. The following is a 4 scene movie that can play 4 audio files that were created as outlined below. By pressing the green button in any scene, you can start the sound object looping. The red button will stop it. The buttons at the bottom allow you to change the volume levels. You will see that you can have unique volume levels for all your sounds across all 4 scenes. You can start and stop the sound objects and still maintain the levels. You can track the levels across scene changes as well.

How It Works

This solution allows you to associate each sound object with its own movie in your main FLA (which is what provides independent control of each sound). These will not be movies you have on stage on the timeline. These will be movies you create using the createEmptyMovieClip method. If you use actual movies, like an empty movie you drag out from the library, you will lose independent control of the sound when you change scenes. The reason this happens is because the movie you so lovingly placed on the timeline of scene 1 does not exist in scene 2, etc. Even if you place the same movie in scene 2, it will not cooperate like you want. Follow the steps below for a working solution.

Creating the Sound Object SWF in Its Own FLA
This process is similar to the steps you would take if you want to preload sound objects that use the attachSound method. Do the following:

  1. Create a new FLA blank document.
    Make the document a small size. I often make these FLA's 5-pixels square. However, you may decide to make the document large enough to add a small button for testing the sound. After you loadMovie the SWF, you can move its X,Y axis off stage so it will not be seen. I'll show code for that in this tutorial.

  2. On frame 1 of the FLA created in step 1, add the following code:

    _root.sound1 = new Sound(this);
  1. Import one sound to the library, right-click it and select "Linkage" and then select "Export for ActionScript". It should look like the following image with both boxes checked as shown below.

  2. Set the linkage ID to "sound1a". You can actually set it to whatever you want. However, to match the example in point 2, use "sound1a". If you want to see more details regarding this step, see How to Define a Sound Object Using the attachSound Method.
  3. Check the FILE / PUBLISH settings to make sure the SWF will be exported with the name you want, and then create the SWF. In this example, it should be set to "sound1.swf". The next one will be "sound2.swf" and so on.
  4. Repeat the above steps to create a separate SWF for each sound that will require independent control. Each sound object should have a unique name (e.g. "_root.sound1", "_root.sound2, etc.). The name of the linkage ID is not important to its ability to maintain independent control, so use any legal linkage ID name.


Prepare the Master FLA to Use the Sounds

In the following steps, we will create empty movies clips ("containers") dynamically and then load the sound SWF's into these movies.

Decide on a Preload Method
I recommend the MovieClipLoader class as a convenient way to handle pre-loading all of these files. The traditional preloader would require the use of loadMovie or loadMovieNum to load the sound SWF's. The MovieClipLoader class has its own method called loadClip. The MovieClipLoader method is shown below.

The follow assumes that nothing is on frame 1 of scene 1 except the preloader and anything else needed to support the preloader graphically. In this example, we are loading 4 sound SWF's. However, the pattern should be obvious enough so that you can reproduce this example for more or fewer sounds.

  1. On the frame 1 of scene 1 of the _root timeline, place the following code:

if (_root.soundLoaded != 1) {

// This (the above) will keep it from loading again if you return to
// this frame by accident.


// Make MovieClipLoader --------------------------
_root.my_MCL = new MovieClipLoader();
_root.preload = new Object();
_root.preload.onLoadStart = function(targetMC) {

trace("started loading " + targetMC);

};// Close onLoadStart
_root.preload.onLoadProgress = function(targetMC, lBytes, tBytes) {

loadingContent = Math.round((lBytes/tBytes)*100);

};// Close onLoadProgress

_root.preload.onLoadComplete = function(targetMC) {

trace(targetMC + " finished");

containersLoaded += targetMC.getBytesTotal();

// Move the loaded SWF's off stage
container1._x = -100;
container2._x = -100;
container3._x = -100;
container4._x = -100;
container5._x = -100;

};// Close onLoadComplete



Notice that in the above code, the X axis of the sound SWF is moved off stage after it loads. This is so it will not be seen during playback.

Make the containers for the sounds

In this example, I show how to load 4 sound SWF's. Follow the pattern to create the number of containers needed for your sounds. The following code should be placed on frame 1 of the _root timeline, just below the previous code.

// Make sound containers

_root.createEmptyMovieClip("container1", getNextHighestDepth());
_root.my_MCL.loadClip("sound1.swf", "container1");

_root.createEmptyMovieClip("container2", getNextHighestDepth());
_root.my_MCL.loadClip("sound2.swf", "container2");

_root.createEmptyMovieClip("container3", getNextHighestDepth());
_root.my_MCL.loadClip("sound3.swf", "container3");

_root.createEmptyMovieClip("container4", getNextHighestDepth());
_root.my_MCL.loadClip("sound4.swf", "container4");

Update the Preloader

  1. Place a text box on stage with the variable name of "loadPercentText" and its type set to "Dynamic".
  2. Place a rectangle on stage that will serve as your loading bar, and press F8 to make it a movie.
  3. Give the loading bar the instance name of "loadBar".
  4. Place the following text directly below the previous text on frame 1.

this.onEnterFrame = function() {

mainTimeLine = Math.floor (_root.getBytesLoaded() / _root.getBytesTotal() * 100);

if (loadingContent > 0) {

loadingContentRounded = Math.round(loadingContent);
loadPercentText = (loadingContentRounded + mainTimeLine) / 2 + " % ";

loadAll = (loadingContentRounded + mainTimeLine) / 2;
loadBar._xscale = loadAll;


if (loadAll > 99 && doOnce != 1) {

doOnce = 1;
_root.soundLoaded = 1;


} // Close onEnterFrame

}// Close if _root.soundLoaded != 1

The above concludes the tutorial.


Previous Next
How to Access MP3 ID3 Tag Data Using Sound Objects How to Troubleshoot Sound Objects
Return to Index Do you want all FLA's used for this tutorial? Do you have a question?

This document copyright © 2006 by Kenny Bellew of Cowfly.Com Design,