|
|
||||||||||
| Ultrashock Tutorials > Flash 8 > Dynamic OOP Music Player | ||||||||||
|
||||||||||
|
|
Dynamic OOP Music Player |
|
||||||||
3. Code ExplanationsNow it's time for us to take appart the code and explain what it does: import Library; Here I’m importing a library class (we will create this later) that I use to keep methods (functions) I reuse for multiple classes, and the Macromedia tweening and easing classes to add a little animation to the player. OOP is about writing reusable code; you need to try to keep only the methods related to what you are doing in each class. class MusicPlayer At the beginning of this block we’ve defined MusicPlayer as a new class; then proceed to define all “class properties” in the class body. Class properties are properties (variables) outside of any class methods, which is why they are defined in the “class body”. Note that you can only set these properties to numbers and strings in the body, once we have created the constructor you can set all other different types of data just as you can a normal property (as long as there isn’t a data type issue). //--------------------------------------------------------------------------- As the comment says, this is the class constructor. Incase you don’t know what a constructor, it is a method that is run once every time a new instance of the class is created. Remember when you were making the Flash file and I told you how the MusicPlayer class has three parameters, this is where they are passed from the Flash file to the class. Once the constructor has been called I define the class properties that couldn’t be set in the class body since we didn’t have any of the parameters yet, and initialize a few methods to get things started. Note: The reason I subtract one from the init parameter when setting __trackid is because I use the loadNextTrack method to start the initial track and it automatically adds one to the current track id. //--------------------------------------------------------------------------- private function changeVolume():Void Onto class methods! The first method as the name suggests, changes the volume of the tracks. To add a little more life to things I’ve made the bar scale smoothly according to the position of your mouse when the bar is clicked. This is accomplished by using a ratio of the _xmouse divided by the bar’s width. The bar’s mask is resized smoothly using the tween and easing classes we imported at the beginning of this class. private function setupUI():Void This is where all the events are setup. The onRollOver and onRollOut actions for all the controls (play, pause, previous track, next track) are the same, which is why I’ve decided to use a for loop with the same actions that runs once for each movie clip inside the controls movie clip. By now you’ve probably noticed the use of the Library class and an unmentioned method called delegate. If your familiar with Macromedia’s Delegate Class you know that delegate is used to fix object scope. I’m doing the same thing as Macromedia, except mine has the ability to pass parameters to the callback method. I will go into more detail about the delegate method later on after we’ve finished the MusicPlayer class. private function pauseTrack():Void Pausing a sound is actually recording the position (in milliseconds) of when the track was paused, keeping it from playing and it can then be re-started from the current position. private function getTrackID(direction:Number):Number This method is used to return the ID of the next or previous track, depending on the direction specified. I’ve added a check to see if ID goes below zero, and if so the number of total tracks is returned, otherwise I use the modulo assignment operator when setting the ID to return to ensure the ID stays below the total tracks. The reason for these checks are so I can create an endless loop instead of having to go through the tracks one at a time to get from the first to the last track and visa-versa. //Updates status of the play and pause buttons according to if paused or not To prevent from doing the same thing twice I don’t allow users to click the play button when playing or the pause button when paused, all other buttons do not need this since they need to be able to be pressed multiple times. Which button is disabled depends on whether the current track is paused or not. Once a determination has been made the method proceeds to disable the button using the enabled state and set it to its hover frame to make it different from the other buttons. private function playTrack():Void The first method, playTrack, checks if the track is paused or not, if so the track is re-started from the pause position, otherwise it will be started from the beginning. beginLoad is used by both loadNextTrack and loadPrevTrack to set everything up for a load. A new sound object is created each time a new track is loaded to prevent sounds from overlapping. After the track has finished it runs loadNextTrack again creating a loop. private function loadNextTrack():Void Loads previous and next tracks, simple huh? private function positionBar():Void This function updates the scale of the bar once every frame to mark the accurate duration of the track. A lot of people this is not possible to stream sound without using ID3 tags since Sound.duration returns the duration of how much of the track has loaded. The full duration cannot be calculated until the file has fully loaded. Another solution is to use the TLEN ID3 tag, the problem was that MP3s have this tag encoded and it wouldn’t work at all if this tag couldn’t be read. Instead I finally worked out a solution by taking the position divided by duration divided by percent loaded to get the correct ratio to scale the bar. private function loadBar():Void The same method is applied for the load bar, except the ratio for the bar is simply the percentage loaded. //Returns ID of the current track playing. This becomes useful if you are trying to access the ID of the current track playing outside the class. You can do this by using player.trackid in the Flash file.
|
||||||||||
©2006 Ultrashock.com - All rights reserved |