Stop Audio

Stop Audio

Flash CS4 ActionScript 3.0 Tutorial

 

How to Stop Audio with Flash ActionScript

 

 

Once you have added audio, there will come a time that you want it to stop. Whenever audio is added, it continues to play until it hits the end of the audio clip or you tell it to stop.

Tutorial Elements

Audio file (mp3) in an external folder.

Button with instance name of stopAudio_btn.

 

Step One

In a new flash document, add two new layers.

Step Two

Name the top layer Actions, the next layer Audio and the bottom layer Button.

Step Three

Choose File > Import > Import to Library.

Step Four

Navigate to your mp3 file and choose Import.

Step Five

Open the Library (Window > Library).

Step Six

Select the first keyframe of your Audio layer and drag the mp3 file from the Library to the stage.

Step Seven

Add a Button Symbol to the Button layer and assign it an instance name of stopAudio_btn in the Properties panel.

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.

stopAudio_btn.addEventListener(MouseEvent.CLICK, onStop);

function onStop(e:MouseEvent):void{

SoundMixer.stopAll();

}

Step Ten

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The audio starts playing. Once you click the button, the audio stops.

Dissecting the Code

The first line of the code at Step Nine adds an event listener to the button. The button is waiting to be clicked. Once the button is clicked, it runs the function called onStop. The function tells the SoundMixer to stop all sounds.

AIF or MP3

This method works with .aif files (Mac) or mp3 files (Mac or PC).