How to Use Flash MX Sound Objects

This document copyright © 2005, 2006 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com
How to Start, Stop and Loop a Sound Object Return to Index Do you want all FLA's used for this tutorial? Do you have a question?

The syntax for starting a sound object is as follows:

firstSound.start([SecondOffset,loop])

It is commonly used as follows:

_root.firstSound.start(0,999);

In this example, "firstSound" is the instance name of the sound object. The "secondOffset" refers to the amount of time, in seconds, that you would like to skip before starting to play the sound (this will be described in more detail later).  The "loop" refers to the number of times you want the sound to loop. In the above example, this would start firstSound at its beginning position of zero and loop it 999 times.

 Fig. 03: Example of starting and stopping

The syntax for stopping a sound object is as follows:

firstSound.stop(["idName"])

It is commonly used as in the following example:

_root.firstSound.stop("firstSound01")

Alternately, the ActionScript could also appear as:

_root.firstSound.stop();

In the first example above of stopping a sound object, the linkage identifier "firstSound01" is used. If the linkage identifier is not used, all sounds will stop. If only one sound should stop, it must be associated with a unique movie clip (container). How to associate the sound object to a movie clip is described in the next section.  

For example, if you want to stop one of three sounds that are currently playing, and the sound was not assigned to a specific movie clip when it was defined, then you must specify the linkage identifier name as part of the stop command.

If you associate the starting of a sound with an event like a button press, it is a good idea to keep the sound from playing if it is already playing (unless you want multiple copies of the sound object playing at the same time). For example:

on (press) {
if (playing!=true) {
  playing=true;
  _root.firstSound.start(0,999);
}

And when the sound is stopped by a button:

on (press) {
  _root.firstSound.stop("firstSound01");
  playing=false;
}

In the Flash example Fig04 below, notice how the sound object was defined (as described above the start buttons) and the result of pressing the various buttons (as described below the stop buttons).  Press both green play buttons to have both sounds running at the same time.  Experiment with the various combinations.

Fig. 04: Example of various effects of different ways of defining and stopping a sound object

 

Previous Next
How to Organize Sound Objects How to Use a Single Button both to Start and Stop a Sound Object
Return to Index Do you want all FLA's used for this tutorial? Do you have a question?

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