Import Audio from External File

Import Audio from External File

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 an external mp3 audio file to the project with ActionScript.

Tutorial Elements

Audio file (mp3) in an external folder. The folder should reside at the same level as your .fla and swf and be named Audio. Name the file inside the folder , Audio01.mp3.

 

Step One

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

Step Two

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

Step Three

Copy/Paste the following code to the ActionScript panel.

var audioTrack01:Sound = new Sound()

var audioChannel01:SoundChannel = new SoundChannel();

audioTrack01.load(new URLRequest("Audio/Audio01.mp3"));

audioChannel01=audioTrack01.play();

Step Four

Save your .fla file. There should be a folder called Audio at the same level that contains a mp3 file named, Audio01.mp3.

Step Five

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 Three establishes a new Sound variable called audioTrack01.

The next line establishes a new SoundChannel variable named audioChannel01. Line three imports the external audio file in the Audio folder. The "/" is used to define the path down into the folder.

The final code sets the audioChannel01 variable to the audioTrack01 variable and starts the music with the play() method.

How Do I Stop the Music?

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

 

Lynda.com career skills