Reacting to Dynamically Loaded MP3s
Want to trigger a set of actions to execute when a sound has finished playing? Want to know a sound's duration (play length) or current position (playback position)? Flash provides you with precisely this type of dynamic control when using loaded MP3s (or even internally attached sounds).
NOTELesson 17, "Scripting for Sound," discussed many of the ways that you can control sounds dynamically.It's often useful to know when a sound will finish playing. Consider a presentation in which the next screen of info should be displayed only after the voiceover has finished explaining the current screen. Or a music jukebox that automatically loads the next song when the current one has finished playing. You can easily achieve this type of functionality by using the onSoundComplete event handler:
Using this syntax, you would define a series of actions to be triggered when the sound in the Sound object finishes playing. The Sound class also provides two other events, onLoad and onID3, which we'll discuss in the next exercise.
mySoundObject.onSoundComplete = function(){
//actions go here...
}
NOTEA Sound object must exist in your movie before you can define an onSoundComplete event handler to it. If you delete or re-create the Sound object, you must redefine the onSoundComplete event. In other words, you can't attach an event to an object that doesn't exist, and after an object has been deleted or re-created, the attached event ceases to exist as well.Every Sound object has a duration property, representing the sound's length (in milli seconds). Accessing this property's value is accomplished in the following manner:
This script sets the value of soundDuration to the duration of the sound currently in the referenced Sound object. If the sound is 5.5 seconds long, the value of soundDuration is set to 5500 (1,000 x 5.5). This property exists for loaded sounds as well as sounds attached to the Sound object by using attachSound().You can determine how far a sound has progressed in its playback by using the following syntax:
var soundDuration:Number = mySoundObject.duration;
If the sound in the referenced Sound object has been playing for three seconds, soundPosition is set to a value of 3000 (1,000 x 3).
var soundPosition:Number = mySoundObject.position;

Open virtualaquarium7.fla.We'll add some script to Frame 1 of this file from the preceding exercise, as well as add a simple script to the progress_mc movie clip instance (which is below the panel graphic).With the Actions panel open, select Frame 1 of the Actions layer and add the following lines of script at the end of (but within) the if statement of the changeSlide() function definition:
slideSound.onSoundComplete = function(){
changeSlide(currentSlide + 1);
}

This script attaches an event handler to the progress_mc instance. For each onEnterFrame event, the value of progressAmount is updated to a percentage value between 0 and 100. This percentage value is determined by evaluating the current position and duration values of the slideSound Sound object and then rounding the result. This value is next used to move the instance's timeline to the appropriate frame. Together, these actions emulate the functionality of a progress bar.The if/else statement makes the progress bar invisible when progressAmount has a value of 100, as when the last image/sound has been shown/played. The progress bar will be visible anytime this value is less than 100 (whenever a sound is still playing).Choose Control > Test Movie.If you let the project play by itself, after the first loaded sound finishes playing, the project automatically advances to the next image/sound and then repeats this process. Notice that the progress bar tracks the playback of the sound as well.
progress_mc.onEnterFrame = function(){
var progressAmount = Math.round(((slideSound.position / slideSound.duration) * 100));
this.gotoAndStop(progressAmount);
if (progressAmount == 100){
this._visible = false;
}else{
this._visible = true;
}
}
