How to Invoke Actions When a Sound Object Ends |
|
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 |
» 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. |
Download (6170 kb) |
» Forums |
More help? Search our boards for quick answers! |