Playing Sounds in ActionScript 1
       by kirupa  |  16 February 2003

During your course of Flashing, you may have added sounds the old-fashioned way. You would probably have added the sounds by adding them to a frame and looping them 999 times. You are no longer a beginner Flasher. You have to move away from relying on frames and, instead, rely on using ActionScript for your sound needs.

Note: This tutorial covers how to play sounds using ActionScript 2. For playing sounds using ActionScript 3, click here.

Why use ActionScript when frame based sounds work perfectly? While sounds controlled by frames work perfectly also, you do not get the level of control over the sound as you would when using ActionScript. By using ActionScript, you are able to change the sound's speaker directions, pans, volume, and more. Best of all, you can create virtual sound environments by using various random numbers and values that change how your sound feels throughout your animation.

The following example is a basic setting in which a sound plays when a button is clicked. The sound is entirely controlled by ActionScript!

[ hint: press the 'play sound' button to hear the sound :) ]

Because this tutorial requires the use of sounds, I will provide you with a sound file that you can download and use in your animations.

Download Sound Loop

Here is how to add sound:

  1. Make sure you unzip the world.zip file you downloaded (WinZip is a good program to unzip files). Create a new document in Flash.
  2. Go to File | Import. The Import dialog box will appear. Browse for and select the sound file you unzipped and press the Open button. The sound file has been imported into your Flash movie.
  3. Press Ctrl + L or go to Window | Library. The Flash Library will appear. You will notice the sound file you just imported contained in the Library. Right click on the sound file's name 'world' and select Linkage. The Symbol Linkage Properties dialog box will appear:

[ the symbol linkage properties dialog box ]

  1. In the Symbol Linkage Properties dialog box, select the "Export this symbol" option. Once you select the Export this symbol option, the Identifier field will be enabled. You should enter a name that will uniquely identify this sound file. Let's simply identify the sound file as "coolsound". Enter coolsound in the Identifier field:

[ the sound file has been given a unique identifier name ]

  1. Once you entered coolsound for the Identifier value, press OK to close the Symbol Linkage Properties dialog box.
  2. Let's go back to the main timeline. Draw a button. To create a button, simply draw a circle. Select the circle, press F8 or go to Insert | Convert to Symbol, and select the option for Button and press OK. You should know how to create a button by now...because you are a Masta Flash Programma!
  3. Once you have created the button, right click on the button and select Actions. Copy and paste the following section of code:
on (release) {
kirupaSound = new Sound(this);
kirupaSound.attachSound("coolsound");
kirupaSound.start(0, 99);
}
  1. Save the FLA and preview the animation. Click the button you just created. If all went well, you should hear the music play.
     

How Does the Code Work?
The following section will basically deconstruct how the code you copied and pasted enables sound to come out of your PC's speakers.


on (release) {
kirupaSound = new Sound(this);
kirupaSound.attachSound("coolsound");
kirupaSound.start(0, 99);
}

The first line is the On handler for the button. The code following this statement will be enabled and executed when the mouse clicks on the button and releases the button. You can access these options by going to Basic Actions | On Mouse Event from the Object Actions window.


on (release) {
kirupaSound = new Sound(this);
kirupaSound.attachSound("coolsound");
kirupaSound.start(0, 99);
}

In this line, I am creating a new sound object. I'm calling the sound object I am creating "kirupaSound". The words new Sound(this); are telling flash to create a new sound object. This line of code is very similar to declaring a variable and giving it a value. Of course, in Flash, you don't have to declare variables before using them.


on (release) {
kirupaSound = new Sound(this);
kirupaSound.attachSound("coolsound");
kirupaSound.start(0, 99);
}

Remember the name you gave to the sound file in the Symbol Linkage Properties dialog box? Well, here is where you actually use that Identifier name you entered. You are telling Flash to attach the sound file identified as "coolsound" to the kirupaSound sound object you created in the previous line.


on (release) {
kirupaSound = new Sound(this);
kirupaSound.attachSound("coolsound");
kirupaSound.start(0, 99);
}

This line tells Flash to actually play the sound. The original syntax is kirupaSound.start();. Why am I not using the original syntax? The original form of the start sound control, objectName.start() is very limited in its use. The sound starts, plays once, and stops. That method is great for short bursts of sounds. What if you have a loop?

The expanded version is objectName.start(offset, loops);. The offset is the number of the seconds in the sound file that Flash will start from. The loops is the number of times the sound will continue playing. In my example, the sound will start from its default position of 0 and loop 99 times before stopping.


Downloads
Never to leave you surprised or confused, I have provided the final source file for this tutorial. If you still have questions, you can post them in the forum by clicking the Message Board link near the bottom of the tutorial.

Final Source for this Tutorial

Conclusion
This is just the beginning of your journey to using sound in Flash. In corollary tutorials that I will create, you will expand upon this method of adding sounds. You will learn how to control the sound volume coming from each individual speaker and more! Before I forget, I'd like to give credit to Asim Khan for creating the Asian sound loops used in both the tutorial and in my example.

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.