Playing Sounds in Flash

You might want to play sounds in different situations such as:

  • A When a button is pressed
  • B When a certain frame in the movie is reached
  • C A soundtrack i.e. music playing continuously in the background
  • D You might want to include instructions in your actionscripts to make sounds play when certain things happen such as a lunar lander crashing.

Before we go any further, be aware that there are two kinds of sounds in Flash:

  • Event sounds play independently of the movie. They must download completely before they can be played.
  • Streaming sounds are sliced up into chunks that are attached to individual frames of the movie. This helps the sounds to stay synchronised with the corresponding frames and also means that the sound(s) can begin to play after just a few frames have been loaded.

A) Playing sounds when a button is pressed

1. Make sure the library panel of your document is open (Ctrl-L)

2. Import a sound into your document’s library with File > import. You can find some MP3 sounds in My Computer > Classes > Ward > ICT all levels > MP3 Sounds. Choose a sound no more than a couple of seconds long.

3. Test the sound by selecting it in the library then pressing the play button above the waveform.

4. Open the common library ‘Buttons’

Window > Common Libraries > Buttons

5. Drag a Button onto the stage

6. Using the selection tool (black arrow), go into symbol editing mode by double-clicking the button on the stage.

7. Add a new layer and rename it ‘Sound Effect’

8. Click where the Sound Effect layer intersects the Down column and insert a blank keyframe (right-click)

9. Drag the sound from the library onto the stage (notice the line that appears in the selected ‘down’ frame.

10. To leave symbol editing mode, click ‘Scene 1’ above the timeline. The timeline should return to its normal state;

11. Press Enter or Ctrl-Enter to test.

B) Playing a sound when a certain frame in the movie is reached

1. Make sure the library panel of your document is open (Ctrl-L)

2. Import a sound into your document’s library with File > import. You can find some MP3 sounds in My Computer > Classes > Ward > ICT all levels > MP3 Sounds. If you already have a sound attached to a button then use a different sound for this purpose.

3. Make your movie at least 48 frames long. If your movie is not already at least that long then insert a frame (not a keyframe) by right-clicking frame 48 in the timeline and choosing ‘insert frame’.

4. Add a new layer and rename it ‘Sound Effect’.

5. Insert a frame in the Sound Effect layer so that this layer contains the same number of frames as the other layer.

6. With the Sound Effects layer still selected, insert a keyframe somewhere between frame 1 and frame 48 (right-click and choose ‘insert keyframe’. Only keyframes can carry the instruction to begin playing a sound.

7. Make sure the keyframe is selected by clicking on it. Its properties are now displayed in the properties panel.

8. In the Properties Panel, select the sound you wish to begin playing when this frame is reached, then set the Sync setting to start. Notice that the waveform of the sound now appears in the Sound Effects layer. Leave the Repeat setting at 1 so that the sound plays once only each time the sound is reached (it’s possible to have sound play several times or even indefinitely if you choose Loop).

9. Press Enter or Ctrl-Enter to test.

10. Since the sound can continue to play even after the movie ends or loops it is important to learn also how to stop the sound playing. Insert a keyframe in the Sound Effect layer about half way through the sound’s waveform then select the sound in the frame’s properties then set the Sync property to Stop.

11. Press Enter or Ctrl-Enter to test.

C) Playing a soundtrack i.e. music playing continuously in the background


D) Playing sounds using Actionscript

In this section I'll explain how you can make a sound play using ActionScript. I'll use ActionScript to play a sound when a button symbol is clicked. Of course, it is also possible to attach sound to a button without using ActionScript as shown in part A above, but using a button is still a simple way of introducing the ActionScript procedures. The important thing to realise is that the procedures below allow you to play sounds anywhere in your ActionScript, such as when a lunar lander crashes, or a player fires a gun, or a slot machine simulator gives a jackpot combination... The assumption is here is that the sound will be very short - this would not be a good method to begin playing background music, for example.

  1. Start a new project.
  2. Using File>Import>Import to Library, import a short sound file in mp3 format (the best sound format for use in Flash) such as this one. If you cannot see the library panel then press Ctrl-L - you should see the sound listed in the library. Remove the extension from the name and if the sound file has any spaces in its name then remove the spaces - this makes it possible to use the same name in the various stages described below. Please remember that Flash is case-sensitive - be sure that the three name you will give are all exactly the same, including capital letters.
  3. The name modified in the previous step is the file name. We will also be creating a 'sound object' with its own name and a link from the file to the sound object - the link has its own name called the 'linkage identifier'. Obviously it would be a good idea to use the same name for for each of these three entities (file, link and sound object). Right-click the sound file in the library and choose 'Linkage...' and then 'Export for Actionscript'. The option 'Export in first Frame' will be automatically turned on - do not turn it off. As previously discussed, set the linkage identifier to be the same as the sound file name.
  4. We need a button to attach the sound to. You could take a ready-made button from Window>Components>User Interface>Button or Window>Common libraries>Buttons or you can make your own button. To make your own button you could make a circle, convert it to a symbol by pressing F8 and then select 'button' behaviour rather than movie-clip behaviour.
  5. Now we just need to add a few lines of script to your button. Make sure the button is selected then open the Actions panel. Copy and paste the following script, replacing 'mysound' with the name of your own sound.
on (release) {
mySound = new Sound(this);
  1. The second line of the above code creates a new sound object called 'mysound' (don't forget to change this to match your own sound's name).
  2. The third line attaches the sound object to the sound file in the library via the linkage you set up earlier.
  3. The fourth line plays the sound (in Actionscript you 'start' the sound rather than 'playing' it).
  4. Press Ctrl-Enter to test your movie.

As an example, here is a button from Window>Common libraries>Buttons>Classic Buttons>Arcade Buttons. This button, like many of the ready-made buttons, has a 'down state' picture included so that it behaves realistically when clicked. It plays an mp3 sound called 'mySound'.

Sounds can be played anywhere you want in your movie - they don't have to be tied to buttons. For example, you could make an explosion sound play if you crash the lunar lander in the lander project, just as you may have done in Visual Basic. Just put the three key lines above at the right point in your project's ActionScript code.

There are thousands of free MP3 sounds, including special effects and music, available free on the internet. Here are some addresses to get you started. > sound effects for a wide selection of sound effects in mp3 format (more than 70 000 free songs). Please be aware that an MP3 song lasting several minutes can occupy several MB of disk space.