Pause/Play Sound

Playing, Stopping, and Pausing Sounds Using AS3

Tags: , ,

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.

Related Posts

ActionScript 3 Logo

Moving From AS2 to AS3: A Handy List of Resources

Welcome to ActionScript 3.0

ActionScript 1:1 – Getting Intimate With ActionScript 3.0

SWF Framerate Optimization

Smart SWF Optimization with Custom Framerates

ActionScript 3 Migration Cookbook

ActionScript 3 Migration Cookbook

8 Comments

  1. Jeff

    03.17.2009

    Reply

    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?

    • Angel

      03.17.2009

      Reply

      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. Benjamin Reid

    03.19.2009

    Reply

    Great tutorial, clear and great source files. Tweeted the link. :)

    • Angel

      03.19.2009

      Reply

      @Ben: Thanks for noticing!

  3. Pandu

    04.06.2009

    Reply

    Great tutorial, I using this on my client site http://www.brigadegroup.com

    • Angel

      04.07.2009

      Reply

      I am glad to see that you got some good use out of it.

  4. Elias Hickman

    05.12.2009

    Reply

    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.

  5. Fahim

    04.01.2011

    Reply

    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

Leave a Reply









Latest Theme From WooThemes

Unsigned

By WooThemes

Unsigned is a hugely powerful, cleverly developed, yet super easy theme to use for musicians and bands looking to showcase their music, tours, photos, videos and band members all from within WordPress. It even allows you to sell your albums and merchandise thanks to the WooCommerce integration. It truly is a massive theme that will cover your every need.

Theme Categories: Multimedia, Responsive

Latest Tweets