How to Use Flash MX Sound Objects

This document copyright 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com
How to Preload Multiple Dynamic Sound Objects using the loadSound Method Return to Index Do you want all FLA's used for this tutorial? Do you have a question?

The following code might be used if you have a lot of dynamic sounds that you do not want to stream (for quality reasons, perhaps). It requires one button per sound. However, it will use only one load bar graphic. Here is what it might look like when you are finished.

Fig. 22: Preloading multiple dynamic sound objects
  1. Create a new layer on the _root timeline, and name it "Sound Objects". Insert the following code on frame 1. Increase or decrease the number of sound objects depending on your needs; however, follow the numbering scheme shown below.
mySound1 = new Sound();
mySound2 = new Sound();
mySound3 = new Sound();
mySound4 = new Sound();
  1. Create a new layer and name it "buttons". On this layer, place one button on stage per sound object. Place the following code on button 1:

// Button 1
on(release) {
_root.nextSound=1;
_root.playMe=1;
_root.mySound1.loadSound("Enigmate-Asim_Kha-2032_hifi.mp3",false);
}

On button 2:

// Button 2
on(release) {
_root.nextSound=2;
_root.playMe=1;
_root.mySound2.loadSound("Chil-Nemo-6522_hifi.mp3",false);
}

On button 3:

// Button 3
on(release) {
_root.nextSound=3;
_root.playMe=1;
_root.mySound3.loadSound("Infin-calpomat-6135_hifi.mp3",false);
}

By now, you should understand the pattern of change for each button. Change the value of _root.nextSound to equal the button number. Similarly, change _root.mySoundn to equal the button number.

  1. Create a new layer and name it "loadBar", place a long thin rectangle on stage that is the width you want your load bar graphic at 100 percent. Select the outline and delete the outline. Press F8 and convert it to a movie, naming it "loadBar". Make the instance name "loadBar" as well.
  2. Make a stop button, and place it on the buttons layers, using the code, on(release) {stopAllSounds();}
  3. Press CTRL-F8 to insert a new symbol. Select Movie Clip, and give it the name "emptyMc." Return to the _root timeline, and drag an instance of emptyMc onto the stage and place it anywhere. Select this new movie so that you can see its Actions panel.
  4. Paste the following code into the Actions panel of the emptyMc instance.

onClipEvent(load) {
_root.loadBar._xscale=0;
_root.nextSound=0;
}
//
onClipEvent(enterFrame) {
 mySoundBytesTotal =_root["mySound" + _root.nextSound].getBytesTotal();
 mySoundBytesLoaded=_root["mySound" + _root.nextSound].getBytesLoaded();
if (mySoundBytesLoaded>0) {
 mySoundLoading=Math.round((mySoundBytesLoaded/mySoundBytesTotal)*100);
if (mySoundLoading==null) {mySoundLoading=0;}
 _root.percentLoadedText=mySoundLoading+"%";
 _root.loadBar._xscale=mySoundLoading;
}
if (mySoundLoading==100 && _root.playMe==1) {
 _root.playMe=0;
 _root["mySound" + _root.nextSound].stop(); // Redundant for example
 stopAllSounds(); // Remove if you have other background sounds that shouldn't stop.
 _root["mySound" + _root.nextSound].start();
}
}

This works because every time you press a button, you change the value of _root.nextSound. That value tells the preloader which sound object it should watch and report.

Previous Next
How to Build a Preloader for Dynamically Loaded MP3's 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?

This document copyright © 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com