Recipe 13.8 Performing Actions When a Sound Ends

13.8.1 Problem

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

13.8.2 Solution

Define an onSoundComplete( ) method for the Sound object.

13.8.3 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.

13.8.4 See Also

Recipe 13.6 and Recipe 13.9

    Part I: Local Recipes
    Part II: Remote Recipes