Friday, 12 September 2008

Create a sound button

This Flash tutorial will teach you how to create a sound button. This involves using two basic buttons (on/off). The sound button will be incidental sound which means that sound/audio is played as a result of an event or action. In this case, it would be clicking the play button.

There are three basic stages in creating a sound button: Importing an audio file into the library, creating the two sound buttons, and Adding code to the buttons.

Some knowledge of basic buttons is required for this tutorial. I will be using an actionscript 2.0 file. And I have used free stock music which is available at: www.sounddogs.com

UPDATE: Actionscript 3.0 version available here.

Create a sound button

Step 1

Open a new Flash document. Import your audio file into the library by selecting File > Import > Import to library select your file and then click ok.

Now right click on your audio file in the library and select linkage. And enter the following settings:




**This assigns an identifier to an object. This allows us to attach an instance of a symbol to the stage at runtime.


Step 2

Create two basic buttons on the stage: one for on and one for off. For more information on buttons checkout the basic buttons tutorial. I have created two basic circular buttons, but you can create whatever design you wish.



Now convert your buttons into symbols by pressing F8. Give your symbols an appropriate name, check button and click ok.

**You could alternatively use the built in button library for this step. This avoids the hassle of creating your own buttons. To use the built in library select Window > Common library > Buttons and then drag the button you wish to use onto the stage.


Step 3

Give your two buttons the instance names: on_btn and off_btn respectively.




Step 4

On the timeline insert a new layer called “Actions”. Right click on the first frame of that layer and select actions. And add the following code:

var my_sound:Sound = new Sound();
my_sound.attachSound("sound_id");

on_btn.onPress = function() {
my_sound.start();
};

off_btn.onPress = function() {
my_sound.stop();
};

**The first line of code creates a new sound object. And the attach sound function identify the id parameter to the sound object. This specifies which audio file that will play. The start and stop function basically start and stop the audio file.


Step 5

Test your movie clip Ctrl + enter.



You should now be able to create sound buttons.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP