Sound Monitoring

[ LiB ]

The Sound object has two very useful properties that tell you how far along a file is when playing the sound and how long the file is. These properties are sound.position and sound.duration .

NOTE

TIP

If at any time you think that your file is making your movie too heavy, try adjusting its compression settings. If worse comes to worst, just make it a mono filewho needs stereo when your whole movie is bogged down? To make your file a mono file, right- click it in your Library window and adjust the Export settings.

Remember when I told you that if you divide the portion into the whole it would yield a percent amount if multiplied by 100? Well, if you divide position by duration

while the sound is playing, you can find out what percent the sound has played while it's playing

GDA_PROG10.7.fla is another demo I created for this chapter. It plays a long sound while displaying what percentage of the sound has been played. Let's jump into the listing to understand how it works.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // In this demo you are shown // how to create a sound object // so you can control sounds // from within ActionScript. // Create the new Sound object mySound = new Sound(); // Attach a sound to play from the library mySound.attachSound("LongSound"); // Play the sound mySound.start(); onEnterFrame = function() {   // Calculate what percent of the file has been already played.   var percent = (mySound.position/mySound.duration)*100;   display = Math.floor(percent)+"%"; // Display to a text box. }; 

The listing starts out with the usual: a Sound object is created, sound is then attached, and finally, the sound is played.

I have also prepared an onEnterFrame handler so that I can check the status of the sound that's playing during every frame. In the first line within this handler I decided to calculate the percentage of what has been already played. I used the following formula:

 var percent = (mySound.position/mySound.duration)*100; 

I divided the part into the whole then scaled the result by 100 to get the percent. Simple enough, so let's move on.

I then rounded down on the result of percent and then concatenated a percent symbol, as the value is being prepared for display and will be converted to a string value.

display is merely the variable that was attached to a textbox that is currently on stage.

Another way that you can detect if a sound has finished playing is by using the onSoundComplete callback function. This event handler alerts you when a sound is done playing. This works great for loops . For instance, if you had a loop going for 30 spins , it would be tedious tracking them with the sound's position and duration. With onSoundComplete , you can just wait until the functions called for you. Demo GDA_PROG10.8.fla shows you just that.

Observe the following listing. It allows the program to play the sound and then alerts the listener when it's done. This demo is pretty useless for practical purposes, but it's very handy when synchronizing data.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // In this demo you are shown // how to create a sound object // so you can control sounds // from within ActionScript. // Create the new Sound object mySound = new Sound(); // Attach a sound to play from the library mySound.attachSound("LongSound"); // Play the sound mySound.start(); // Setup Display display = "Sound Playing..."; // Detect when it's done playing mySound.onSoundComplete = function () {   display = "Sound Complete!"; }; 

Besides creating and attaching and then playing the sound, the code calls the onSoundComplete function as soon as mySound finishes playing the sound. As a sign that it has finished, I set it to display a message on a textbox on the stage. The attached variable is called display .

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net