This is the first of two music playing Flash objects that we will build, both of which read information (data) from an external source. This one takes a "juke box" approach—a variety of songs are available in any order, but one must press the appropriate button to make it work. The second player will be more of an "iTunes/Winamp" style player—it plays the songs in sequence but there is only one play/pause button and you can skip ahead and backward.
For the juke box you will need more than just a Flash file; you will also need several mp3 files (obviously) and a data file. If you don't have any songs handy I've put five song snippets along with a sample data file and a Flash file with some ready-made buttons together in a zip file that you can download and use.
&title1=Toccata in D&&artist1=J.S.Bach&&song1=tunes/BachEdit.mp3&
Notice that I named my variables title1, artist1 and song1. Notice also that the variable values aren't surrounded by quotation marks. Also note the ampersands—each variable has one at the beginning and end.
For the second song, do the same thing except change the variable names to title2, artist2, and song2:
&title2=5th Symphony&&artist2=L.V.Beethoven&&song2=tunes/BeethovenEdit.mp3&
Add a new line for each song, changing the variable numbers accordingly.
You can set the font style, size, and color to whatever you like (make sure you pick a text color other than white if your player has a white background!). Bare in mind the the text has to be small enough and/or the fields large enough to contain the variable information (titles & artist names).
loadVariablesNum("music.txt", 0);
This of course loads the variables stored in the text file into Flash. Why not just put all the variables in Flash to begin with? Well, once the Flash movie is completed and exported you can't get back into the file to edit the Actionscript. To update songs you would have to change the code on the original Flash file & export it again, and that's assuming you have the original file. With this method, however, you can update the songs (and song information) by simply changing the text file—which is easy to do; as long as you use the same variables, you can substitute any songs you like.
on (release) {
stopAllSounds();
mySong = new Sound();
mySong.loadSound(song1, true);
titleWindow = _root.title1;
artistWindow = _root.artist1;
}
The stopAllSounds(); command does what you'd expect—stops any and all songs that might be playing; it's important to do this before we start a new song or we will get noise. mySong = new Sound(); creates a container object within the project to hold the sound. mySong.loadSound(song1, true); grabs the first song and puts it into the sound object we just created. "True" in this case means that the song is set to stream (begin to play before it's fully downloaded). If you want your sound to download completely before it plays you would write "false," but that's not recommended for this project. The artistWindow line loads the artist1 variable into the first text window; the titleWindow line loads the title variable into the title field.
on (press) {
stopAllSounds();
artistWindow = "";
titleWindow = "";
}
onClipEvent(load) {
this._x=_root.hBase._x + _root.hBase._width/2 - this._width/2;
left=this._x - _root.hBase._width/2;
top=this._y;
right=this._x + _root.hBase._width/2;
bottom=this._y;
volCalc=_root.hSlider._x - _root.hBase._width/2
}
onClipEvent(enterFrame) {
this.onPress = function () {
startDrag(this, false, left , top , right, bottom)
}
this.onRelease = this.onReleaseOutside = function () {
this.stopDrag();
}
sliderx=_root.hSlider._x;
audioVolume=(sliderx-volCalc);
_root.mySong.setVolume(audioVolume);
}
The first part of the script is triggered when the slider knob movie clip loads. It calculates the width and height of the base movie clip, divides those measurements in half, and then places the knob in the middle of the base. It also sets the maximum amount that the knob can be moved left & right, top & bottom. Finally it creates a variable called volCalc and assigns a value to it equal to the position of the slider (so since the slider starts at 50 on a 100 pixel base, volCalc is given a value of 50).
The second part of the script (enterFrame) is constantly running (unlike the onLoad which only runs once). This script does two things: first it makes the knob "draggable;" second it creates a variable called audioVolume which determines the position of the slider and then assigns that value to the volume of the sound. As the value drops towards zero, the volume drops. As it increases towards 100, the volume increases.
4 pts |
Portfolio quality design; great jukebox. |
3 pts |
Good looking; above average work. |
2 pts |
It works, but it's nothing fancy. |
1 pts |
Something's not working right. |
0 pts |
Poor showing; mostly incomplete or full of errors. |