Flash Tutorial Links:
Play Games: HTML5 Tutorials:

How To Add Sounds To Flash

Your Flash game would be very dull and boring without a background music and sound effects. Learn to add them into your Flash game easily.

Add Sounds and Music Dynamically

In this tutorial, we will learn how to add sounds into your Flash game. You'll not do it via the timeline like traditional animators would, but rather, add or remove them dynamically using Actionscript 3.

Importing the Sounds First

Before you can use your code to add them into your running game, you need to first import them into your library in the game FLA file (File > Import > Import to Library > Choose your sound files). So go scout around, preview some sound/music files and save them to the same project folder as your FLA file (doesn't have to be in the same folder so to speak, but it helps to get things organised). I recommend you create a snd folder for it, like how I created a img folder for storing images.

After you import them in, you'll see that they're sitting in your library like this.

sound files in library

You can see that their Linkage is currently empty, meaning you can't use them dynamically with code. We'll fix this. Right click on the sound file and select Linkage.

Setting the Linkage

For the class name, use something meaningful. Since I know that this sound file is an explosion sound, I'll just call it SoundExplosion. That's all you need to do for now.

sound linkage

Now, go back to your usual GameController.as file, or probably even just type in these codes in your FLA file. I'll show you how it can be done just inside the FLA file first.

Play the Sounds Effects

We will handle sound effects separately from the looping background music, although the difference is very small. Let's see how we can play sound effects first.

var snd_explosion = new SoundExplosion();
snd_explosion.play();

The code that you need to play a sound in Flash is surprisingly easy. Your eyes aren't playing tricks on you, it's just these 2 lines of code! We first create a new object from the class SoundExplosion (remember we set this earlier on via the Linkage panel), and assign it to a variable called snd_explosion. Once that is done, to play the sound wherever we want, you just need to call .play() on the variable.

If you want the sound to play twice, just use play(0,2). The first parameter, 0, indicates that you want to start playing the sound from the 0th position in the sound file. If, for some reasons, you want to start your sound playing 1 second into the sound file, then just use 1000 (it takes in milliseconds) as the first parameter. The second parameter, 2, indicates that you want to play the sound twice, and Flash will play them consecutively for you.

Play the Music

We need to handle the playing of music a little differently, because you may want to give the player an option to pause the game music, or perhpas mute it. Playing of sound effects is done such that we play and forget about it, but since only 1 stream of the music will run, we need a more permanent way to manipulate it. SoundChannels are used to manage this. For the code below, assume that I've created another sound file, and called its class name as SoundMusicLoop.

var snd_musicLoop = new SoundMusicLoop();
var sndChn_musicLoop = new SoundChannel();
 
sndChn_musicLoop = snd_musicLoop.play(0,int.MAX_VALUE);
 
sndChn_musicLoop.stop();

You can see that when we now issue the play command to the snd_musicLoop, we store the return object (which is actually a SoundChannel) into sndChn_musicLoop. This variable now stores the reference to the sound channel which our music is still playing.

Also, rather than ask Flash to play the music for 10 times, 1000 times, or even 1000000 times so that it creates the illusion of a looping sound, we simply use int.MAX_VALUE, which will provide you with a very large number such that you can realistically assume that it will loop forever

To Mute The Music

With this SoundChannel, we can do some interesting things to our background music. Of course, we can stop it if we want simply by calling .stop(); now.

var snd_musicLoop = new SoundMusicLoop();
var sndChn_musicLoop = new SoundChannel();
 
sndChn_musicLoop = snd_musicLoop.play(0,int.MAX_VALUE);
 
sndChn_musicLoop.stop();                     

Or to more appropriately mute it, we need something called a SoundTransform object. The code to set the volume of your music down to 0 is shown below.

var snd_musicLoop = new SoundMusicLoop();
var sndChn_musicLoop = new SoundChannel();
 
sndChn_musicLoop = snd_musicLoop.play(0,int.MAX_VALUE);
 
var snd_Transform = new SoundTransform();
snd_Transform.volume = 0;
sndChn_musicLoop.soundTransform = snd_Transform;

Play around with this value, and you can amplify the sound by making it sound louder by 2 times, simply by setting it to a value of 2.

Incorporating Sounds into An External AS File Like GameController.as

The code shown so far is coded in the FLA file. Ideally, you should be doing it in the GameController.as which is the style I've been trying to show so far. Let me show you what modifications you have to make for that.

Firstly, you need to import in the relevant packages as shown in line 3.

package
{
    import flash.media.*;
    
    public class GameController extends MovieClip
    {
        private var snd_explosion, snd_musicLoop:Sound;
        private var sndChn_musicLoop:SoundChannel;
        
        public function GameController()
        {
            //...
            
            //Prepare the sounds when the game starts
            snd_explosion = new SoundExplosion();
            snd_musicLoop = new SoundMusicLoop();
        }
        
        public function startGame()
        {
            sndChn_musicLoop = snd_musicLoop.play(0,int.MAX_VALUE);
        }
        
        //...
    }
}                    

Lines 7 and 8 are where you declare these sound variables as class attributes so that you can use them anywhere in your code. When the game is started, the constructor GameController is called. This will be a good place to prepare the sounds by instantiating the sound objects and assigning them properly to these variables. Alternatively, these can be placed in the startGame if you want, provided you don't need the sounds anywhere else, say the Menu?

When we start the game, the startGame function is called, and that's where we play the music loop and assign it to the proper channel, which is sndChn_musicLoop. The codes to mute it, or stop the music can be placed inside the functions that will eventually trigger them.

And there you have it! Make your Flash games more lively with sounds!

Flash Resources
Preloader FPS Display Sounds & Music
Keycodes Name Generator
Game Development Resources
Sprite Sheets

Flash Tutorial Links:
Play Games: HTML5 Tutorials:
gaming tools download on app store now!
Home | About Us | Contact Us
Powered by Make Flash Games. All Rights Reserved.
By Joseph Tan