Import Audio from Library

Import Audio from Library

Flash CS4 ActionScript 3.0 Tutorial


How to Add Audio to Flash .fla File with ActionScript



Audio can be added to a project using multiple methods. The easiest method is to import the audio the the Library, select a keyframe, then drag it to the stage.

Other methods include importing audio from an external mp3 file or importing the audio from the Library with ActionScript. In this example, we will import the audio to the Library, then add it to the project with ActionScript.

Tutorial Elements

Audio file (mp3) in an external folder


Step One

In a new flash document, name the default layer Actions.

Step Two

Choose File > Import > Import to Library.

Step Three

Navigate to your mp3 file and choose Import.

Step Four

Open the Library (Window > Library).

Step Five

Select the audio file in the Library and choose Properties from the Library panel pull-down menu.

Step Six

If necessary, click the Advanced button to expose the bottom half of the dialog box.

Step Seven

Click Export for ActionScript and enter a class name of Audio01. Once finished, click OK.

Step Eight

Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Nine

Copy/Paste the following code to the ActionScript panel.

var audioTrack01:Audio01 = new Audio01();

var audioChannel01:SoundChannel = new SoundChannel();;

Step Ten

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The audio starts playing and plays only once.

Dissecting the Code

The first line of the code at Step Nine establishes a new Audio01 variable called audioTrack01. Audio01 is the Class you assigned in the Library in Step Seven.

The next line establishes a new SoundChannel variable named audioChannel01. The final code sets the audioChannel01 variable to the audioTrack01 variable and starts the music with the play() method.

AIF or MP3

Using this method you can import either .aif files (Mac) or mp3 files (Mac or PC).

How Do I Stop the Music?

This method allows you to start the music. For instructions on stopping your audio, see topic, Stop Audio.