Jaswinder:Play sound

Flash - Play Sound

To add sound to your flash movie is very easy. Just import a sound file (in whatever format the flash editor can handle, e.g. mp3, wav... etc), and pull it onto the stage! However, if you want to have better control in playing the sound file, you may need some actionscripts.

Below is an example where you can start and stop the music by pressing the corresponding start & stop button.

Step 1

Open a new file in the Flash editor. Import a sound file into the library. Right click on the imported sound (in the library window; you may need to open the library window first), select link on the pop up menu. Check the export to actionscript option and type the name "mysound" in the textbox. This will assign an id to the imported sound, so later on the actionscripts can refer to it.

Step 2

Ok, back to the main stage. Select the first frame on the timeline, open the action window and put the following code in it:

var snd = new Sound; snd.attachSound("mysound");



Basically, the above code will create a sound object and attach the imported sound file to it.

Step 3

Insert a new clip/button symbol and name it as "playbtn". Draw whatever you want the button look likes. Then move back to the main stage, drag the button symbol from the library window (place it any where you like). Select the "playbtn" on the stage and put the following code in the action window:

on(release) { _root.snd.start(0, 10); }

This will start playing the sound (and loops 10 times) when the button is clicked.

Step 4

Repeat step 3 to create the stop button (name it as "stopbtn"), but associated with the following actionscritps instead:

on(release) { _root.snd.stop; }

Thats all. Test your movie and Enjoy!

< Tutorials Index - Download this tutorial - Post suggestions>