First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
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() { 
_root.myNextSong.loadSound("sample02.mp3");
_root.myNextSound.start();
}

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.start();
playing=true;
_root.myTextBox="Playing"
}
_root.firstSound.onSoundComplete = function() {
_root.myTextBox="Complete"
playing=false;
}
}

The stop button:

on (press) {
if (playing==true) {
_root.firstSound.stop("firstSound01");
playing=false;
_root.myTextBox="Stopped";
}
}

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
(10 being the highest)
» 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!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.