Playing, Stopping, and Pausing Sounds Using AS3

In a previous tutorial, I demonstrated a simple solution to loading an external sound file. In that example, the mp3 file that had been loaded begins to play automatically. While this might be fine in some cases, you probably would want more control over the sound being imported.

For example, being able to stop or pause then play again might be a good place to start.   In this tutorial, I will be demonstrating how to play, stop, and pause sounds using AS3 once you have the sound loaded into Flash.

Playing a sound

Playing a sound using AS3 is a simple as calling the play() method of the defined sound object. Previously in AS2, you would have called the start() method so not much different in this area.

soundClip.play();

Stopping a sound

Stopping a sound using AS3 is accomplished by calling the stop() method of the defined sound object. This is exactly the same way you would be able to handle it using AS2.

soundClip.stop();

Pausing a sound

This is where pausing a sound using AS3 is quite different from using AS2. As it stands now, there is no method to automatically pause a sound using AS3. Instead, you must populate an optional parameter of the play() method.

It allows you to play the sound using an offset value that is passed in from a stored variable. Below is an example of accomplishing this.

//Stores the position of the "playback head"
var pausePosition:Number = sndChannel.position;
sndChannel.stop();

//When ready to resume playback, call this line
sndChannel = soundClip.play(pausePosition);

Putting it all together

Pause/Play SoundNow it is time to integrate all three.  I have provided an example that puts all three of these control functionalities into good use.  The example is sessentially a simple music player with toggle functionality.

Preview example

Download source

Subscribe to the RSS Feed to stay updated on future tutorials on using sound in AS3.

I hope this helps in your migration over into  AS3.  If there is something in particular you want to add or see with the Moving From AS2 to AS3 series, feel free to contact me via the contact page.

9 thoughts on “Playing, Stopping, and Pausing Sounds Using AS3

  1. Nice Tutorial. What do you do though, when you have more than one sound. Say you have two sounds that each have a set of play / pause / stop controls. Each in a separate movieclip as well. At first I thought to put SoundMixer.stopAll();inside of the play functions but that doesnt work out so well. This is what happens:

    I have for example sndA and sndB, say I press sndA’s play button, sndA plays. Then I press sndB’s play button and sndA stops as sndB begins. But then if I go back and press sndA’s play button nothing happens unless I press sndA’s stop button first. Then i can press sndA’s play button and it will play sndA and stop sndB but now sndB’s stop button must be hit in order for its play button to work. the other problem with SoundMixer.stopAll(); is it stops ALL. I dont want all to be stopped, only the sndA or B files.

    This is strange and I dont quite get it. I have been reading everything I can to figure this out. I have found that SoundChannel.stop will work, but I need to control SoundChannel in a different movieclip. I have been trying things like:

    MovieClip(Parent).sndB.SoundChannel.stop; this hasnt seemed to work so far either.

    Any ideas?

    • Hmmm! I am not sure. I think I have an idea, however, it will be easier if I can see a file. If possible, email the file to angel [at] flashspeaksactionscript dot com

  2. I am indebted to you this amazing contribution.
    I nested your buttons and almost all of your code inside another movieclip on the stage and when I go to that frame I have to click on the start button instead of just being able to enter the frame and have the movie start like I used to.

    Can you shoot me an email so that I can send you the AS script that I’m using with and more details. I know it’s something very simple.
    Thanks.

  3. Hi, My name is Fahim I am new in AS3 can you please help me in adding volume slider in the above tutorial

    how to add

Comments are closed.