Loading an external mp3 file in Flash has many benefits. First, and maybe the most practical is that your Flash movie filesize stays the same – there is no need to import the sound file into it, because we can load it dynamically, that is, while the movie is running. Second, if you wanted to change the music, you can change just the mp3 file, without having to open the Flash .fla file to modify it. There is also the possibilty to make the music change automatically, or to build an mp3 player inside Flash, but I’ll leave that for another, more advanced tutorial.
1 Open a new Flash document. Save your Flash movie in a folder created specifically for this exercice. Now copy an mp3 file you’ll use with this project in the same folder where you just saved the Flash document.
2 Back in Flash, call the current (and so far, the only one) layer mc. In this layer, we will put an empty movie clip which will load our external mp3 file.
Creating an empty movie clip
3 Choose Insert > New Symbol, choose Movie Clip as type of symbol, call it sound holder and click OK. Now, if you look above the timeline, you’ll see Scene 1 written and sound holder to the right of it. This indicates that you are now inside the sound holder movie clip. To make an empty movie clip, click on Scene 1. That’s it. Now go check the Library (choose Window > Library if your Library panel isn’t open yet) and you’ll see the empty sound holder movie clip in it.
4 Drag and drop the newly created sound holder movie clip onto the scene. You will see it represented by a small circle. This is how empty movie clips look like in Flash. Now, in the Properties panel, just below the scene, you should see Movie Clip written on its left side. Below this inscription is an input field, with words Instance Name written inside. Click this input field, write soundLoader in it and press enter. Now, you have just named your movie clip.
Without the instance name we just entered, we wouldn’t be able to control the movie via ActionScript. Choose whichever name you like instead of soundLoader, but the important thing is to stick with it throughout all of the ActionScript code you will write in this Flash document.
When giving a movie clip on stage his Instance name, it is important to follow a few simple rules. Your movie clip Instance name cannot contain spaces or any other special characters like !$%# etc. Also, the Instance name must not begin with a number. Avoid putting an underscore at the beginning, too. Use letters and numbers, that will suffice. Be careful: ActionScript is case-sensitive. That means the name soundLoader isn’t the same as soundloader or Soundloader. The Library name of a movie clip (we called it sound holder) can contain spaces and other special characters. Why? Because this name isn’t important for ActionScript. It is with the Instance name that we control our movie clip.
Lock the mc layer. We now have our target movie clip for sound loading. Let’s move on to ActionScripting.
5 Create a new layer, call it actions. Click on the actions layer’s first frame and open the Actions panel by choosing Window > Actions or pressing F9 (PC) or ALT+F9 (Mac). Type in the following code:
coolTune = new Sound (soundLoader);
Test your movie. You should hear the mp3 file play. If you can’t hear it, either you didn’t follow the tutorial properly or you didn’t turn the speakers on 🙂 Just kidding… check if you typed in everything correctly, unlock the layer with the movie clip, if you don’t see it, find it by pressing CTRL+A (PC) or Command+A (Mac). Click on it once, and check the Properties panel – see if you got the Instance name correctly. Now, let’s see what do the different pieces of our code mean and do.
coolTune = new Sound (soundLoader);
This line creates a Sound object. In Flash, creating a Sound object is necessary if you want to control a sound via ActionScript. So, we basically tell Flash that
coolTune is a new Sound object (you can call it whatever you like – coolTune, coolSound, song01, etc. But, the Sound object needs to be associated with a movie clip to function. So, we wrote the Instance name (soundLoader in this case) between parentheses following the
new Sound bit of code. Now that we defined a new Sound object and associated it with a target movie clip, we can start to tell it what to do.
loadSound method (a method is a thing that an object can do, like for example, a real object can do things – a car can accelerate or steer) is used for loading an external mp3 file. This method has two parameters / the url (location of the file) and whether the mp3 file will be streamed or not. I will explain these two in more detail now.
If your mp3 file happens to be in the same folder as your Flash movie (as is the case in this tutorial) you just write the name of the file (with the extension .mp3) between quotation marks. If you have your mp3 files in a subfolder, called, let’s say, music, you would write the url as follows:
"music/somesong.mp3". See the images and what would the code for these situations be like below.
You probably now this already, if you ever linked any pages manually in HTML. It is just about finding the path to the mp3 file.
The second parameter between the parenthesis determines whether the sound (our mp3 file) will be streamed or not.
If you have (like in our case) this parameter set to
true, it will be streamed. This means that the mp3 file will begin to get loaded in your Flash movie, and it will start to play once a sufficient portion of it has been downloaded. Then, it will continue loading as it plays. This is ideal for songs, background music, etc.
If it is set to
false, the sound will play only once it has been loaded completely. This is best used for smaller files, like sounds for a button, a menu being opened etc. There is no sense in using this with an mp3 file which is a few megabytes big. The visitors to your site won’t hear anything until it has loaded completely, which may take 5 minutes or more, depending on the speed of their Internet connection. It would be great if everyone had a rad T1 connection (drool, drool), but that is not the case in the real world.