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 it comes to using loaded MP3s (or even internally attached sounds).

NOTE

Many of the ways sounds can be controlled dynamically were discussed in Lesson 16, Scripting for Sound.


First, a brief explanation of how these actions work.

It's often useful to know when a sound will finish playing. Consider, for example, a presentation where the next screen of info should only be displayed once the voiceover has finished explaining the current screen. Or how about a music jukebox that automatically loads the next song once the current one has finished playing. You can easily achieve this type of functionality by using the onSoundComplete event handler. As is the case with other event handler methods, you can use two types of syntax, the first of which is as follows:

 mySoundObject.onSoundComplete = function(){    //actions go here…  } 

Using this syntax, you would directly define a series of actions to be triggered when the event occurs. The second syntax looks like the following:

 mySoundObject.onSoundComplete = mySoundFunction; 

Using this syntax, you would define the name of the function that should be executed once the event occurs useful if that function is employed elsewhere (that is, called from other sources in your movie) and you simply want to make a call to it when the event is triggered.

NOTE

A 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 once an object has been deleted or re-created, the attached event ceases to exist as well.


NOTE

There is also an onLoad event handler that you can use in similar fashion to the onSoundComplete handler just described. Its purpose is to trigger a function when a loaded sound has finished loading. This event is only triggered if the loaded sound is set to load as an Event sound. Unfortunately, the beta version of the program we were using at the time of this writing produced unexpected results. Look for this event to function properly in the release version.


A sound's duration is a property of any Sound object, representing the sound's length (in milliseconds). Accessing this property's value is accomplished in the following manner:

 myVariable = mySoundObject.duration; 

The above script sets the value of myVariable to the duration of the sound currently in the referenced Sound object. If the sound were 5.5 seconds long, the value of myVariable would be set to 5500 (1000 x 5.5). This property exists for loaded sounds as well as ones attached to the Sound object using attachSound() .

You can determine how far a sound has progressed in its playback by using the following syntax:

 myVariable = mySoundObject.position; 

If the sound in the referenced Sound object has been playing for three seconds, myVariable will be set to a value of 3000 (1000 x 3).

graphics/17fig19.gif

In this exercise, we'll use the onSoundComplete event to trigger a function. We'll also employ the duration and position properties of the Sound object to create a playback progress bar.

  1. Open virtualaquarium7.fla in the Lesson17/Assets folder.

    This is the file we worked on in the previous exercise. In this exercise we'll add additional script to Frame 1, as well as add a simple script to the progress movie clip instance (which is below the panel graphic).

  2. With the Actions panel open, select Frame 1 of the Actions layer and add the following lines of script to the end of (but within) the if statement of the changeSlide() function definition:

     slideSound.onSoundComplete = function(){       changeSlide(currentSlide + 1);  } 

    graphics/17fig20.gif

    This defines what should occur when the sound loaded into the slideSound Sound object has finished playing. You'll see that a call is made to the changeSlide() function. This acts as an automatic advancing mechanism for the slide show: As soon as a sound has finished playing, the next image and sound are loaded.

    Let's finish the exercise by scripting the playback progress bar. First, however, let's look at its structure.

  3. Double-click the progress movie clip instance to edit it in place.

    This movie clip is made up of three layers. We're most interested in the bottom layer, named bar, which contains a 100-frame tween. If you drag the playhead, you'll see that this tween is set up to emulate a progress bar with a range of 0 to 100 percent. In a moment, we'll add a script to move this timeline to the appropriate frame based on the percentage of the sound file that has played. In other words, if 47 percent of the sound file has played, this clip will be at Frame 47, showing the appropriate progress.

  4. Return to the main timeline. With the Actions panel open, select the progress movie clip instance and attach the following script:

     onClipEvent(enterFrame){    progressamount = Math.round(((_root.slideSound.position /  _root.slideSound.duration) * 100));    gotoAndStop(progressAmount);    if (progressAmount == 100){      _visible = false;    }else{      _visible = true;    }  } 

    By now, this script should be fairly familiar. Every enterFrame 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, then rounding the result. This value is next used to move the instance's timeline to the appropriate frame, all of which emulates the functionality of a progress bar.

    The if/else statement is used to make the progress bar invisible when progressAmount has a value of 100, as when the last image/sound has been shown/played. Anytime this value is less than 100 (whenever a sound is still playing), the progress bar will be visible.

  5. Choose Control > Test Movie.

    If you let the project play by itself, you'll see that once the first loaded sound finishes playing, the project automatically advances to the next image/sound, then repeats this process. Notice that the progress bar tracks the playback of the sound as well.

    graphics/17fig21.gif

    After the third image/sound has loaded and finished playing, the presentation stops and the progress bar disappears because there is no more content to load, as defined in the slides array. Only by pressing the left-arrow button can you bring the presentation to life again.

  6. Close the test movie. Save the file as virtualaquarium8.fla.

    This completes the exercise and this lesson!

    You've now created a fairly sophisticated project that loads external assets and can be easily scaled to include an almost limitless amount of external content. By loading content into a movie as it plays, you need never worry about huge files or stale content again.

    This also marks the end of the book. Congratulations! You now have a very strong grasp of not only what ActionScript allows you to do with your projects, but more importantly, how to use it to implement fairly complex interactivity in your own unique projects.

    Although we've covered a lot of ground in these seventeen lessons, you should continue to hone your skills as a scripter. ActionScript is a very extensive scripting language, therefore it is impossible for us to cover every use or possibility. This is where your creativity comes into play. As mentioned at the very beginning of the book, while most people might not see it as such, scripting is an art, where your task is to develop creative solutions for the task at hand. You now have a great foundation for doing so.

    You'll continue on your own to learn powerful ways of accomplishing tasks using ActionScript, but it's also important to look at the code of scripters with a bit more experience than yourself. Doing so will give you insight to how those with years of scripting experience approach a task, and you'll learn many new tricks that you can use in your own work to make it more efficient and fun. That's the keyword, 'fun'! You'll find that the more you know how to use ActionScript, the more you'll enjoy doing things with it. So get out there and show the world what you can do.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net