O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.7. Pausing and Resuming a Sound

Problem

You want to pause and then resume a sound.

Solution

To pause a sound, store the sound’s current position and call the stop( ) method. To resume the sound, call the start( ) method, passing it the value of the sound’s stopping position. Alternatively, you can create custom pause( ) and resume( ) methods to automate this process.

Discussion

The Sound class does not provide built-in methods to pause and resume a sound. However, with a little bit of code, you can achieve the same result. The key is to store the sound’s position property before stopping (pausing) the sound and then use that value to tell Flash at what point to resume playback.

Therefore, to pause a sound:

  1. Get the value of the sound’s position property and store it in a variable:

    pauseTime = mySound_sound.position;
  2. Call the stop( ) method:

    mySound_sound.stop(  );

And when you want to resume the sound, simply do the following:

  1. Convert the stored position, in milliseconds, into a starting offset, in seconds, by dividing by 1000.

  2. Call the start( ) method and pass it the appropriate value for the offset:

    mySound_sound.start(pauseTime/1000);

You can automate the preceding process by creating two custom methods: pause( ) and resume( ). Add the following code to your Sound.as file for easy inclusion in other projects:

Sound.prototype.pause = function ( ) { // Get the current position and then stop the sound. this.pauseTime = this.position; this.stop( ); }; Sound.prototype.resume = function ( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required