ActionScript 2.0 sounds in Adobe Flash
For our first game, I taught you how to do sound by linking a sound and using the startSound actionScript method. You probably used a series of commands like this when you wanted to use a sound.
The first command here creates a new sound object. An object is a container for something. In this case when we create the container, we tell ActionScript that we want this containter to hold a sound.
The second command loads a sound from the library into the mySound object. In order for this to work, we selected the "Linkage" option from the library options info.
The third command loads starts playing the sound. I mentioned before that the number 0 here represents the position at which you want to start playing the sound in seconds. 0 here means play the sound from the beginning, 1 would mean 1 second from the beginning and so on and so forth. The second number (1 in this case), represents how many times you want the sound to repeat.
We don't actually need to put these two numbers in this start command, we can simply type in the command like this:
I wanted you to understand that you can create multiple sound objects for every sound in your movie. When we were making the game, we could load all our sounds into a single sound object, just like we did. It might be more convenient to create a separate sound object for each sound. So we could create sound objects for a wallhit, a blockhit and a paddlehit like this.
//Create all my sound objects
Now whenever you want to use one of these sounds, you can call the start() method of the specific sound object.
This would be better and more efficient than 1. creating a new sound object everytime an event takes place (which is what we were doing for the game), and 2. using the same sound object for all the sounds.
This would also allow us to very easily change a specific sound throughout the entire program, by simply editing the attachSound command above.
Flash can play a maximum of eight individual sounds simultaneously. It's a good idea to create a different sound object for every sound you want to play in flash.
Loading a sound from a file
There are other ways to load a sound into the sound object. Instead of using attachSound(), we can use the loadSound() method to load up a sound from a file. You simply supply a URL to the file location relative to the .swf file or an absolute reference viat a http URL. You can only load MP3 sounds this way.
Find an MP3 in your hard drive, create a new movie and place the following script on an "actions" keyframe in the first layer of the movie
//Create my sound object
mymusic.loadSound("Beautiful.mp3"); //Beautiful.mp3 is the name of my sound;
Then create a button or movie clip on your movie and add the following ActionScript:
Run the movie clip and click on the button. Your sound should start playing. If you wanted to put your sounds in a folder called "music", you would change the loadSound() method like this:
mymusic.loadSound("music/Beautiful.mp3"); //Beautiful.mp3 is the name of my sound;
or if you placed this sound at the root level of planetoftheweb.com, it would look like this:
mymusic.loadSound("http://www.planetoftheweb.com/Beautiful.mp3"); //Beautiful.mp3 is the name of my sound;
Pretty simple. Now let's create a button that stops the song from playing.
Create another button and type this actionscript in there:
Now run the movie and when you press on the second button your music should stop playing.
Multiple tracks playing at the same time
If we continue to press the play button, you'll notice that flash will start playing a new copy of the same sound over and over. A sound won't stop playing until you tell it to (and Flash can play up to eight copies of the same sound), so it might be a good idea to stop the current sound before you start playing it.
To change the sound, you can load a new sound into the same sound object. Create a third button and type in the following script on it:
Here I stop the current song, load a new song and start playing it. You might notice a delay in the playing. The way we have flash set up, the song will start playing after it is loaded.
You can also choose to stream the file so that the song starts playing as soon as enough of it is loaded into flash. To do that you would change add a value of "true" to the loadSound method.
//Create my sound object
mymusic.loadSound("Beautiful.mp3",true); //Beautiful.mp3 is the name of my sound;
This works fine if you want this to be a general background song playing through your movie, but if you want to create a jukebox, it's not a good option because the sounds starts playing automatically (no start() method required), and it doesn't behave properly even when you stop it and try to start it again.blog comments powered by Disqus