Flash MX 2004 MP3 Player Pg.10
source: http://www.thegoldenmean.com/
10 — ActionScript Concluded
Finishing up the ActionScript
Create the XML Object
Before the songListLoaded() function can parse the XML Object, extract the information and populate arrays with that information, there first has to be an XML Object. This bit of script creates an instance of an XML Object and establish its callback function - in other words, what to do when the Object has loaded its data:
plXML = new XML(); plXML.ignoreWhite = true; plXML.onLoad = Delegate.create(this, songListLoaded);
The Setup Panel
First of all, what is this all about? Recall quite a few pages back I mentioned that I personally feel the visitor to your site should have the option of hearing sounds or not. This is an entirely personal decision you get to make for your own site. In my own case, I want the Player to be all ready to play but hold off starting until the user in effect authorizes it to do so.
Once the XML playlist is loaded, the Player starts. You might have noticed that we haven’t loaded the external XML playlist document yet. Between where we are now and that final step I have included script that prevents the Player from running until the user agrees. This is accomplished by placing a stop() action at the very end of this frame’s code and loading the XML playlist document in the next frame (frame 15). The setup panel included a button which instructs the playhead to move to the next frame. Until the user agrees to hear music by clicking the “go” button, the Player stays on this frame - loaded and ready but stationary.
I occurred to me that as long as I had this setup panel in place maybe I could gather some additional information. Consequently the panel allows the user to elect to listen to tracks in linear or random order (even though they can’t see what that order is), and to indicate whether they use a slow or fast connection. (The result of this selection is to modify the _soundBufTime variable which affects how long Flash buffers the stream before the Sound Object begins playing. Flash’s default buffer time is 5 seconds.)
Knowing that this isn’t an option some of you may want to exercise, I made it easy to eliminate. The setup_mc is stored in your library but not physically (if it is possible to use that term with software!) placed on stage. It is attached with code. If you opt not to employ what I term the “courtesy pause”, then for you frame 14 of the script layer is complete. Skip down to loading the XML playlist.
The setup panel involves a considerable amount of code which I will put off to a separate page to make it easier to ignore if you don’t plan to utilize it. If you are interested, the setup_mc code is provided on this page.
For the curious but not fanatical readers out there, the setup panel’s functionality can be briefly summarized. An instance of the panel symbol is attached to the movie. This suggests that at some point it will have to be removed! It is simple enough to just remove it using the removeMovieClip() method but it can be more fun to animate its departure. For this I turn to Flash MX 2004’s splendid easing Tween Classes. (These Classes are based on code Robert Penner developed and published for Flash MX. Just about any property can be animated with the Tween Classes. In the examples I have sprinkled throughout this epic tutorial you have seen implementations in which the panel exits by sliding to the right and sliding down. One mre example remains in which the panel just fades away. This is strictly for fun, but it is fun!)
A cluster of radio buttons accept user input to determine play mode (default is “linear”) and conneciton speed (default is “high speed”). Finally, the “continue” button advances te movie by a frame and the Player begins.
As mentioned previously, if you elect to use the setup panel the movie remains paused at this point, waiting for user input in the setup_mc panel. If you decide to go straight to the music without this pause, simply remove the setup panel code block and the stop() action that concludes this frame, permitting the Player to automatically move on to the next and final frame (frame 15). If you elect to keep the setup pane, then frame 14 concludes with:
stop();
Frame 15 - Load the Play List
Hooray! The last of the ActionScript! Click once in frame 15 of the scripts layer and press F6 to create a keyframe if you haven’t already.
If you elected to use the setup panel, remove it now because it is no longer necessary. Just to clarify: the Tween class used to animate the setup panel’s departure didn’t really remove the instance - it merely moved it out of sight. Having served its purpose there is no point to having it hang around any longer, so just get rid of it and free up some system memory. This directive is wrapped in an if() conditional so there is no harm to leaving this code in even if you decide to by-pass the setup panel.
if (typeof setup_mc == "movieclip") { removeMovieClip(this.setup_mc); }
Now load the external XML playlist file. This is the triggering event that starts the Player. As soon as the playlist has loaded, the songListLoaded() callback function executes and the Player is off and running. Be sure to get the path correct: remember that songList.xml.php resides inside the tracks subdirectory.
plXML.load("tracks/songList.xml.php");
Tip: How can you test locally if your computer doesn’t have Apache and PHP installed on it? Here’s what I have done: assuming you do have access to hosted web space that supports PHP, go ahead and upload your tracks directory (complete with the three PHP scripts and some or all of your MP3 files). Navigate to the songList.xml.php file in a web browser. Depending on your browser, you may have to select View Page Source to see the script’s output. Copy this source and paste into a new document in your text editor. Save this as songList.xml. Now you have a static XML file you can use locally during the development process (modify the line of code above to read
plXML.load("tracks/songList.xml");
but don’t forget to change it to songList.xml.php and re-compile the .swf once you have completed testing your movie and are ready to put it on your server for real).
Stop the movie:
We don’t want the movie to start over again from frame 1, so to prevent the movie from looping (and - congratulations - to conclude the ActionScript) we stop the movie:
stop();
Are we done yet?
Yes!
Maybe…
I recognize there is always the chance that your aesthetic tastes and mine might differ. If you are considering changing the Player’s appearance (Please - I encourage you to do so!!!), move on to skinning the Player.
--top--