Flash Tutorial, Dynamica Sound
In this Flash CS3, CS4 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 and CS4, there are basically three ways to play a sound:
1. Import the sound into the library, then add the sound to the time line.
2. Import the sound into the library and then use ActionScript to control the sound.
3. Use ActionScript to dynamically load and play the sound.
This Flash 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 plugins 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 Flash Tutorial is simply going to delete the .wav from the Class name, thus calling the sound Ring.
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()
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 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()
You should have noticed the stop and play button located in the start01.flv file. The final part of this Flash 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.
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.
Press Control-Enter and test your movie.