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.8. Performing Actions When a Sound Ends

Problem

You want to perform certain actions when a sound finishes playing.

Solution

Define an onSoundComplete( ) method for the Sound object.

Discussion

The Sound class has a built-in event that triggers the onSoundComplete( ) method for a Sound object when the sound finishes playing. This is a convenient place to perform actions when the sound ends. All you need to do is define the onSoundComplete( ) method and Flash will automatically invoke it at the correct time:

mySound_sound.onSoundComplete = function (  ) {
  trace("The sound has finished playing.");
};

The onSoundComplete( ) method is invoked only when the end of the sound is reached. It is not invoked when the sound is stopped by way of ActionScript. Also, onSoundComplete( ) is invoked only when the last loop completes, not each time the sound loops, when a sound’s playback is looped.

See Also

Recipe 13.6 and Recipe 13.9

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