8bitrocket.com
23May/080

Tutorial: AS3 Basics – How to use library sounds in AS3

Now this one sounds like a no-brainer, but for some reason almost every book written on AS3 and even the built-in help fail to give adequate information on how to effectively use sounds embedded in the library in AS3. I scoured the included help file and eventually found how to do this, but I have had a few people ask me how it is done, so here goes. We will also cover looping the sound, playing from an offset, and controlling all of the sounds in an application at once to provide mute functionality.

1. First you need to have a sound in your library. Let's call our sound explode_sound.
2. You must make sure to set explode_sound to export for Actionscript (it will set a base class of flash.media.sound) and an export name of explode_sound
3. In your Actionscript (in a frame, in a class, etc) you need to import the basic sound classes:

[cc lang="javascript" width="550"]
import flash.media.Sound;
import flash.media.SoundChannel;

[/cc]

4. You must then instantiate an instance of your sound in the library:

[cc lang="javascript" width="550"]
var sndExplode:explode_sound;[/cc]

5. You then need an instance of the SoundChannel class to actually be able to hear your sound.

[cc lang="javascript" width="550"]var sndExplodeChannel:SoundChannel;
[/cc]

5.5 You must create and instance of your sound

[cc lang="javascript" width="550"]
sndExplode=new explode_sound();[/cc]

6. To play the sound, you must assign it to your SoundChannel.

[cc lang="javascript" width="550"]sndExplodeChannel=sndExplode.play();
[/cc]

7. You can play a sound from a millisecond offset by adding the first parameter: The example will start 10 milliseconds into the sound.

[cc lang="javascript" width="550"]
sndExplodeChannel=sndExplode.play(10);

[/cc]

8. You can loop a sound by adding the second parameter. The example has no offset and will loop 3 times

[cc lang="javascript" width="550"]sndExplodeChannel=sndExplode.play(0,3);

[/cc]

9. You can control the volume of this individual sound by first importing in the SoundTransform class.

[cc lang="javascript" width="550"]import flash.media.SoundTransform;

[/cc]

And then creating and applying the transform to the sound. This will increase the volume 3x.
[cc lang="javascript" width="550"]
var transform1:SoundTransform=new SoundTransform();
transform1.volume=3;
sndExplodeChannel.soundTransform=transform1;

[/cc]

10. To universally control all of the sounds by calling class methods of the import flash.media.SoundMixer; Class.

[cc lang="javascript" width="550"]
import flash.media.SoundMixer;
flash.media.SoundMixer.stopAll();

[/cc]

And then you can use it easily to set the volume for all sounds in the application like this. Below acts like a mute for all of the sounds in the game or application universally.
[cc lang="javascript" width="550"]
import flash.media.SoundMixer;
var transform1:SoundTransform=new SoundTransform();
transform1.volume=0;
flash.media.SoundMixer.soundTransform=transform1;

[/cc]
To un-mute to application, you change the mixer value to 1.
[cc lang="javascript" width="550"]
transform1.volume=1;
flash.media.SoundMixer.soundTransform=transform1;
[/cc]

Download a sample .fla file
[/cc]

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
This site is protected by Comment SPAM Wiper.