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

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.