FREE Flash CS3 Tutorials
Flash CS3 Tutorial explaning different ways to access sound within Flash
Gordon French is a professional Flash CS3 Program and Designer.
He also attends the Art Institute in Denver where he majors
in Interactive Media and Design.
You will find Flash CS3 Tutorials, Flash Tutorials, and Lots of General FREE Flash tutorials.

Index.html
Flash_Concentration.html
new_in_3.html
Flash_Asteriods.html
what_is_AC.html
history_of_AC.html
AC_index.html Flash CS3 Tutorial Naming Basics
Flash CS3 Tutorial DragAndDrop
Flash CS3 Tutorial SoundBasics
Flash CS3 Tutorial Graphics
http://www.f2-4kids.com

ActionScript

Flash CS3 Tutorial, Sound, Sound, Sound

StumbleUpon Toolbar


Playing Sounds
In this Flash CS3 Tutorial I am going to go over Using Sound, after all what Flash game would be complete with out the great sound effects. In Flash CS3, there are basically three ways to play a sound:

1. Import the sound into the library, then add the sound to a key frame in the time line.
2. Import the sound into the library and then use ActionScript to dynamically control the sound.
3. Use ActionScript to dynamically load and play the sound.

This Flash CS3 Tutorial is not really going to cover the first way to play a sound. You should know how to import an image into your library, you import a sound the same way. And just as you would drag an instance of an image onto the stage, you can also drag a sound onto the stage. The sound will simply play every time the key frame plays. Let work on the second way of playing a sound.


A sound file has already been imported into the library. However, if you wish to use your own audio file you can do so by going to File - Import - Import to Library, and select an audio file . Flash CS3 by default will allow you to import, AIF, WAV and MP3 files. There are additional plug ins that enable more files types. Right Click the audio file called Ring.wav. Select Linkage, Check export for ActionScript, leave the base class alone, but you may change the Class to any name you can remember. This CS3 Tutorial is simply going to delete the .wav from the Class name, thus calling the sound Ring.

Flash CS3 Linkage Panel

Now, something needs to be done to call the sound. As normal select the first key frame on the actions layer and press F9 to open the actions panel. In Flash CS3 using ActionScript you need to create a new instance of just bout anything you want to work with. So, create a new instance of the sound by creating a new variable and setting it to the sound. In the example code the variable phoneRing was created, set as equal to a new instance of Ring. The next line of code actually tells Flash to play the sound. After typing, or copying and pasting the code to your file, you may test it.

var phoneRing:Ring = new Ring()
phoneRing.play()

Continue Smiley
Press Control-Enter to hear your sound play once..


The last method of playing a sound is to load the sound dynamically. The advantage to dynamically load sounds is that the sounds will not increase the size of your .swf, thereby keeping the download times lower. Copy the code from the example and paste it in the actions panel below the code that makes the phone ring. URLRquest is a new method introduced in ActionScript 3.0. The getURL method no longer exists, therefore you may want to get familiar with URLRequest, but not in this Flash CS3 Tutorial. In the example code you are simply telling Flash to go get the file listed in between the quotation marks. You can use http:// if the file is not located next to the .swf. Next in the example code, the variable Music is created and defined as a sound. Lastly, the variable music is loaded with the file from the URLRequest. At this point the sound is loaded but nothing is being done with it. Lets do something with it.

var yourRequest:URLRequest = new URLRequest (“SideOfABullet.mp3”)
var Music:Sound = new Sound()
Music.load(yourRequest)

You should have noticed the stop and play button located in the start01.flv file. The final part of this Flash CS3 tutorial will be using those buttons. Before, ActionsScript can control the sound it needs to have a soundChannel defined. The example code creates a variable called controlMusic and data types it as a sound channel. You can then control the sound by simple calling on the variable controlMusic.

var controlMusic:SoundChannel

Add the final example code to the actions panel. You should be familiar with most of this code. You are simple adding event listeners to the stop and play buttons that were created for you. It is the code inside the events that should be new. First when the play button is clicked the sound channel you created earlier is set a Music.play(). remember you earlier defined Music as being equal to the audio file loaded when the .swf is called. So, the code associated with the play button is simple telling the music to play. It would stand to reason that the code associated with the stop button would then simply tell the music to stop. Just, remember that you have to control the sound channel, and not the sound directly.

play_btn.addEventListener(MouseEvent.CLICK, playMusic)
function playMusic(event:MouseEvent):void{
  controlMusic= Music.play()
};

stop_btn.addEventListener(MouseEvent.CLICK, stopMusic)
function stopMusic(event:MouseEvent):void{
  controlMusic.stop()
};

 

Press Control-Enter and test the file.
Continue Smiley

Congratulations, you should now be able to add sound to your website, note that you do not need to make the buttons play music, you have the basic code to make your navigation play a chime when clicked.

Download Flash CS3 Source Files

Flash CS3 Tutorials, Copyright FrenchSquared 2008
ActionScript