Creating a simple flash sound controller

Author: satz | Date: May 27, 2009

In this tutorial, we learn how to create a simple sound controller using flash. In this, we focus on the Flash sound Object. This article will show how to control an mp3 file in a flash movie. This articles is aimed at the Flash action Script beginners.

Step1.

  • Create a new Flash movie. (File >> New>>Flash Document)
  • Save it. (File>>Save)
  • Import the sound file. (File>>Import>>Import to Library [*wav OR *.mp3])
  • Create/place 2 buttons on the stage (Window>>Components>>User Interface >>Button)

Now our first step of importing the necessary files are completed

Step2.

We have imported the sound file and the necessary navigation control buttons on the newly created flash document. Now we need to make the buttons and the sound file accessible for flash Action script.

  • Go to Library and select the imported sound file. (If not visible in the side panel,  go to Window>>Library).
  • Right click and then choose “Properties”.
  • On the Sound Properties window, go to the “linkage” section towards the bottom of the window.
  • Select the “Export For Action script” check box and check it.
  • Now, the “identifier” text input will be active and place the string, “learn_flash_sound” (Without Quotes).
  • Now Press “OK” button and close the “Properties window”.
  • Select any one of the buttons on the stage and go to its Properties and give the instance name as “sound_play_button” (without quotes). This will be your Play button. So change the button Text into “Play”. [If dragged from components, click on the “parameters” tab beside the properties tab. Change the “Label” into “Play”]
  • Select the next button on the stage and give the instance name as “sound_stop_button” (Without Quotes). [If dragged button from the complonents, do the same step above and change the label into “Stop”]

Now our step of preparing the necessary files for action script control is completed.

Step 3.

Select the first frame in the Timeline and put the following code into it. (Select the first frame, then Right click, and then choose “Actions”).

var my_first_sound :Sound = new Sound();
 my_first_sound.attachSound("learn_flash_sound");
 sound_play_button.onRelease = function() {
 my_first_sound.start();
 };
 sound_stop_button.onRelease = function() {
 my_first_sound.stop();
 };

This code will be explained in the next Step.

Now, our flash Sound controller is ready! Press” Control + Enter” and export the movie into SWF.

Step4.

Action Script Explained:
“var my_first_sound :Sound = new Sound();“
This is just declaring a sound Object and storing it in a variable called “my_first_sound”
“my_first_sound.attachSound(“learn_flash_sound”);”
This is attaching the particular sound file from the library with the identifier, “my_first_sound” (Remember the step 2, .. Export For Action script…)


“sound_play_button.onRelease = function() {
my_first_sound.start();
};”

The Play button is associated with the Sound object’s .start() method
“sound_stop_button.onRelease = function() {
my_first_sound.stop();
};“

The stop button is associated with the Sound object’s .stop() method.

Click here to download the Source file