First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Kenny Bellew

How to Invoke Actions When a Sound Object Ends

Fig. 14: Example of invoking an event at the completion of a sound object

In the Flash example in Fig. 14 (to the right), press play, and notice that the text box reports when the sound completes.

The method "onSoundComplete" allows you to have actions occur upon completion of a sound object. The syntax for this for a sound object with the instance name of "myMusic" is as follows:

myMusic.onSoundComplete = callbackFunction

Commonly, "onSoundComplete" is used as follows:

myMusic.onSoundComplete = function() { 
trace("MyMusic has completed");

If you wanted the next dynamically loaded MP3 to load when myMusic completes, the following code could be used:

myMusic.onSoundComplete = function() { 

In the above example, a sound object with the instance name of "myNextSong", which was defined on the _root timeline, would load the file "sample02.mp3" and start playing it as soon as the file "sample.mp3" completed.

The onSoundComplete method does not need to be called in a movie clip that loops. Once the frame containing the onSoundComplete code has been processed, the function will process its commands every time the sound completes. In fact, if the onSoundComplete method is placed in a one frame loop, it may not work, as the loop is constantly redefining the function.

If the sound starts and ends a second time, the onSoundComplete call will continue activating whatever ActionScript was contained within its function definition. If you don't want the function to call the initial onSoundComplete actions, redefine the onSoundComplete with a new function that does whatever is needed (which might be nothing).

The onSoundComplete method was used often in the creation of the examples shown here. If a sound object is started while the same sound object is playing, the sound object will play on top of itself. If this is not intended, a way to keep the play button from restarting the sound while it is playing is needed. Most of the examples set a variable of "playing=true" when the play button is pressed. The play button also looks to see if "playing!=true" before allowing the play code to process. However, when the sound object completes, the "playing" still equals "true", which keeps the play button inactive even when the sound has completed.

One way to resolve this is to use the onSoundComplete method. Note the following code used for the play and stop button to build the example used in this section:

on (press) {
//Play Button
if (playing!=true) {
_root.firstSound.onSoundComplete = function() {

The stop button:

on (press) {
if (playing==true) {

The uses for onSoundComplete are numerous. In previous versions of Flash, timing an animation to end at the same time a sound ended must have been tortuous. The speed of the computer or internet connection often determined when the animation ended. Now, there is a way to make sure that animation and sound synchronize. Combine onSoundComplete with the new "position" method of the sound object, and suddenly the animation to sound synchronization possibilities are so much easier and more exact.

» Level Intermediate

Added: 2002-08-13
Rating: 8.98 Votes: 664
» Author
Kenny Bellew is a technical writer in Minneapolis, MN. He's currently interested in becoming involved in other writing-related Flash projects.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.