Cooking with ActionScript, Part 2

Editor's note: Last week's sample recipes from ActionScript Cookbook covered using a unique depth when creating a new movie clip, performing actions at set intervals, and more. This week we conclude this series with recipes on pausing and resuming a sound, saving a local shared object, and searching XML. Just a sampling of the hundreds of solutions to common ActionScript problems that you'll find in this book.

Recipe 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:

Get the value of the sound's position property and
store it in a variable:

pauseTime = mySound_sound.position;

Call the stop( ) method:

mySound_sound.stop( );

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

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

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 ( ) {
// Start the sound at the point at which it was previously stopped.
this.start(this.pauseTime/1000);
};

Here is an example that uses the custom pause( ) and resume( ) methods:

// Attach a push button from the Library. You must first drag a push button from the
// Components panel to the Stage to create the Library symbol.
this.attachMovie("FPushButtonSymbol", "myPushButton", 1);
// Create a sound holder movie clip.
this.createEmptyMovieClip("soundHolder_mc", 2);
// Create the Sound object.
mySound_sound = new Sound(soundHolder_mc);
// Attach the sound from the Library. You must have a sound with the linkage
// identifier of MySoundSymbol for this to work.
mySound_sound.attachSound("MySoundSymbol");
// Define two callback functions. One resumes the sound, and the other pauses the
// sound. When each is called, it toggles the click handler for the push button to
// the other function.
function resumeSound ( ) {
mySound_sound.resume( );
myPushButton.setClickHandler("pauseSound");
myPushButton.setLabel("Pause Sound");
}
function pauseSound ( ) {
mySound_sound.pause( );
myPushButton.setClickHandler("resumeSound");
myPushButton.setLabel("Resume Sound");
}
// Define the initial click handler and label for the push button.
myPushButton.setClickHandler("pauseSound");
myPushButton.setLabel("Pause Sound");
// Tell the sound to start.
mySound_sound.start( );