Create a Jazzy XML Driven MP3 Player
ActionScript 3.0 allows Flash Developers and Designers to make all manner of things in Flash with relative ease. This tutorial will teach you how to create your own ActionScript 3.0 MP3 Player with sound spectrum, album art, color changer and song information display.
Step 1
Begin by creating a new ActionScript 3.0 document. Set the Stage size to 600px x 600px and 30 FPS. I've chosen #333333 as my background color.


Step 2
Go to File > Import > Import to Library... and import an image (I've modified part of an iPod from free4illustrator.com for this example. You should use a .png format instead .jpg format to take advantage of the transparency for your object.). After having imported the image, open your Library ( Ctrl + L ) and drag the .png to the stage.


Step 3
Select the image and convert it to a Movie Clip symbol(F8). Name it "Ipod" or another name if you prefer. I've chosen the top left corner as registration point.


Step 4
Double-click "Ipod" movieclip symbol to enter its timeline. Be sure you're inside the movieclip and not "Scene 1".


Step 5
In the Timeline bar, insert a New Layer above the "IpodBody" layer and name it "Buttons Layer". We'll put all our button symbols in this layer.


Step 6
Use the Rectangle Tool, #00ff00 as the fill color and colorless as the stroke color. We don't need a stroke here and in fact, the fill color can be any color you like, it won't be shown in the end result. Draw a shape above the previous icon, similar to the picture below:


Step 7
Convert the shape which you've just drawn into a Button Symbol(F8) and name it "prev" as shown in the picture below:


Step 8
Give the "prev" Button Symbol an Instance Name. I've named it "prevBtn" and changed the Color Style to Alpha with 0%.


Step 9
The following steps will need the same Button symbol as the "prev" button symbol. Copy the "prev" Button symbol and paste it onto the "Next" icon on your bitmap. To duplicate this symbol, go to Properties Bar and click "Swap". A Swap Symbol window will pop out, at which point you need to click the "Duplicate Symbol" (be sure that you are choosing the "prev" button symbol.). Name it "next".


Step 10
Now we have a new symbol which is similar to the "prev" button symbol, but named "next". Give this symbol an Instance name of "nextBtn". The Color Style remains alpha 0%.


Step 11
Repeat Step 9 - 10 by duplicating "prev" symbol to make "play", "pause" and "stop" button symbols. Assign Instance names of "playBtn", "pauseBtn" and "stopBtn" respectively.


Step 12
Insert a new layer above "Buttons Layer" and name it "Text".


Step 13
Draw a text box similar to the picture shown below in the "Text" Layer. Set "Dynamic Text" as the Text Type, assign the Instance name "titleTxt", Arial font face, Font size set to 12px and Bold, white color(#ffffff) and aligned to center.


Step 14
Create another text box just below the "titleTxt" text box. Set this new text box as Dynamic Text, give it an Instance name of "artistTxt", with Arial font face, Font size set to 10px, white color(#ffffff) and, again, aligned to center.


Step 15
Insert a new layer above the "Text" layer and name it "Color Changer".


Step 16
Use the Rectangle Tool and set #ff0000 as the fill color. Draw a small square of 14px X 14px on the right top corner of the ipod screen and convert it to a Button symbol( F8) with a name of "red". Give it a Instance name of "redBtn".


Step 17
Repeat Step 16 and make two more squares. Fill one square with #ffff00, name it "yellow" and assign an Instance name of "yellowBtn". Fill the other square with #66ccff, name it "blue" with an Instance name of "blueBtn". You can set this to any color you like, it will allow the user to change the sound spectrum color.


Step 18
Insert a new layer above the "Color Changer" layer and name it "ActionScript Layer".


Step 19 - Begin the Script
From this moment on, we will be playing with ActionScript 3.0. On the first Keyframe of "Actionscript Layer" press F9. An Action window will pop out. We'll input our script into this window later.


Step 20 - Setting Variables
First, we need to tell Flash what what the ipod Player contains and its properties. There are several variables we need. When the Flash has loaded completely, we need it to start running. The "Loaded" function will start immediately. The loader will load an XML file, the path of which is stated in "URLRequest" of the "loader". We'll create the "musiclist.xml" file for this iPod Player later.
1 |
|
2 |
var musicReq: URLRequest; |
3 |
var thumbReq: URLRequest; |
4 |
var music:Sound = new Sound(); |
5 |
var sndC:SoundChannel; |
6 |
var currentSnd:Sound = music; |
7 |
var position:Number; |
8 |
var currentIndex:Number = 0; |
9 |
var songPaused:Boolean; |
10 |
var songStopped:Boolean; |
11 |
var lineClr:uint; |
12 |
var changeClr:Boolean; |
13 |
var xml:XML; |
14 |
var songList:XMLList; |
15 |
var loader:URLLoader = new URLLoader(); |
16 |
loader.addEventListener(Event.COMPLETE, Loaded); |
17 |
|
18 |
loader.load(new URLRequest("musiclist.xml")); |
Step 21 - Album Art Holder
Later, we'll create album art which displays on the screen of the iPod Player. The following script states the coordinates of the album art, which will be held in a Movie Clip Symbol.
1 |
|
2 |
var thumbHd:MovieClip = new MovieClip(); |
3 |
thumbHd.x = 50; |
4 |
thumbHd.y = 70; |
5 |
addChild(thumbHd); |
Step 22 - Define the "Loaded" Function
Now let's define the "Loaded" function. We want it go to the XML and retrieve the Song URL to be played in the Player. When the song is loaded, it will play immediately and the album art for the song will load simultaneously. When the song has finished playing, it will start to play next song.
1 |
|
2 |
function Loaded(e:Event):void{ |
3 |
xml = new XML(e.target.data); |
4 |
songList = xml.song; |
5 |
musicReq = new URLRequest(songList[0].url); |
6 |
thumbReq = new URLRequest(songList[0].thumb); |
7 |
music.load(musicReq); |
8 |
sndC = music.play(); |
9 |
titleTxt.text = songList[0].title; |
10 |
artistTxt.text = songList[0].artist; |
11 |
loadThumb(); |
12 |
sndC.addEventListener(Event.SOUND_COMPLETE, nextSong); |
13 |
}
|
Step 23 - Define the "loadThumb" Function
Now we'll define the "loadThumb" function. When the thumb is loaded, it will tell Flash to start the "thumbLoaded" function. This function will add a Movie Clip containing the album art.
1 |
|
2 |
function loadThumb():void{ |
3 |
var thumbLoader:Loader = new Loader(); |
4 |
thumbReq = new URLRequest(songList[currentIndex].thumb); |
5 |
thumbLoader.load(thumbReq); |
6 |
thumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded); |
7 |
}
|
8 |
|
9 |
function thumbLoaded(e:Event):void { |
10 |
var thumb:Bitmap = (Bitmap)(e.target.content); |
11 |
var holder:MovieClip = thumbHd; |
12 |
holder.addChild(thumb); |
13 |
}
|
Step 24 - State "EventListener" of Buttons
Here I've stated that the Buttons we created have their respective functions. To allow users to click on the button and execute the function, we use "MouseEvent.CLICK". All the functions of respective buttons will be mentioned in the next few steps.
1 |
|
2 |
prevBtn.addEventListener(MouseEvent.CLICK, prevSong); |
3 |
nextBtn.addEventListener(MouseEvent.CLICK, nextSong); |
4 |
playBtn.addEventListener(MouseEvent.CLICK, playSong); |
5 |
pauseBtn.addEventListener(MouseEvent.CLICK, pauseSong); |
6 |
stopBtn.addEventListener(MouseEvent.CLICK, stopSong); |
Step 25 - Define the "prevSong" Function
"prevBtn" (Previous button) function, define "prevSong":
1 |
|
2 |
function prevSong(e:Event):void{ |
3 |
if(currentIndex > 0){ |
4 |
currentIndex--; |
5 |
}
|
6 |
else{ |
7 |
currentIndex = songList.length() - 1; |
8 |
}
|
9 |
|
10 |
var prevReq:URLRequest = new URLRequest(songList[currentIndex].url); |
11 |
var prevPlay:Sound = new Sound(prevReq); |
12 |
sndC.stop(); |
13 |
titleTxt.text = songList[currentIndex].title; |
14 |
artistTxt.text = songList[currentIndex].artist; |
15 |
sndC = prevPlay.play(); |
16 |
currentSnd = prevPlay; |
17 |
songPaused = false; |
18 |
loadThumb(); |
19 |
sndC.addEventListener(Event.SOUND_COMPLETE, nextSong); |
20 |
}
|
Step 26 - Define the "nextSong" Function
"nextBtn" (Next button) function, define "nextSong":
1 |
|
2 |
function nextSong(e:Event):void { |
3 |
if(currentIndex < (songList.length() - 1)){ |
4 |
currentIndex++; |
5 |
}
|
6 |
else { |
7 |
currentIndex = 0; |
8 |
}
|
9 |
|
10 |
var nextReq:URLRequest = new URLRequest(songList[currentIndex].url); |
11 |
var nextPlay:Sound = new Sound(nextReq); |
12 |
sndC.stop(); |
13 |
titleTxt.text = songList[currentIndex].title; |
14 |
artistTxt.text = songList[currentIndex].artist; |
15 |
sndC = nextPlay.play(); |
16 |
currentSnd = nextPlay; |
17 |
songPaused = false; |
18 |
loadThumb(); |
19 |
sndC.addEventListener(Event.SOUND_COMPLETE, nextSong); |
20 |
}
|
Step 27 - Define the "playSong" Function
"playBtn" (Play button) function, define "playSong":
1 |
|
2 |
function playSong(e:Event):void{ |
3 |
if(songPaused){ |
4 |
sndC = currentSnd.play(position); |
5 |
songPaused = false; |
6 |
}
|
7 |
else if(songStopped){ |
8 |
sndC = currentSnd.play(position); |
9 |
songStopped = false; |
10 |
}
|
11 |
else if(!sndC){ |
12 |
sndC = currentSnd.play(position); |
13 |
}
|
14 |
}
|
Step 28 - Define the "pauseSong" Function
"pauseBtn" (Pause button) function, define "pauseSong":
1 |
|
2 |
function pauseSong(e:Event):void{ |
3 |
if(sndC){ |
4 |
position = sndC.position; |
5 |
sndC.stop(); |
6 |
songPaused = true; |
7 |
}
|
8 |
}
|
Step 29 - Define the "stopSong" Function
"stopBtn" (Stop button) function, define "stopSong":
1 |
|
2 |
function stopSong(e:Event):void{ |
3 |
sndC.stop(); |
4 |
position = 0; |
5 |
songStopped = true; |
6 |
}
|
We have now built the player controls.
Step 30 - State "EventListener" of Colored Buttons
The buttons we created in steps 15-17 are not useless. We now have to tell Flash what their role is within the Player:
1 |
|
2 |
redBtn.addEventListener(MouseEvent.CLICK, changeRed); |
3 |
yellowBtn.addEventListener(MouseEvent.CLICK, changeYellow); |
4 |
blueBtn.addEventListener(MouseEvent.CLICK, changeBlue); |
Step 31 - Define Colored Button FunctionsS
Each colored button has its respective function. "changeRed" will set the line of the spectrum to "0xFF0000" (Red). "changeYellow" will set it to "0xFFFF00" (Yellow) and "changeBlue" will set "0x66CCFF" (Cyan). We also want Flash to know that when we click on Red color, it will inherit 100% opacity while the others will be given 50% opacity
Note: In ActionScript 3.0, 100% = 1, 10% = 0.1 and so on.
All colored buttons will have this function, so we must state the opacity of the button in each function.
1 |
|
2 |
function changeRed(e:Event):void{ |
3 |
lineClr = 0xFF0000; |
4 |
changeClr = true; |
5 |
redBtn.alpha = 1; |
6 |
yellowBtn.alpha = 0.5; |
7 |
blueBtn.alpha = 0.5; |
8 |
}
|
9 |
|
10 |
function changeYellow(e:Event):void{ |
11 |
lineClr = 0xFFFF00; |
12 |
changeClr = true; |
13 |
redBtn.alpha = 0.5; |
14 |
yellowBtn.alpha = 1; |
15 |
blueBtn.alpha = 0.5; |
16 |
}
|
17 |
|
18 |
function changeBlue(e:Event):void{ |
19 |
lineClr = 0x66CCFF; |
20 |
changeClr = true; |
21 |
redBtn.alpha = 0.5; |
22 |
yellowBtn.alpha = 0.5; |
23 |
blueBtn.alpha = 1; |
24 |
}
|
Step 32 - Set Default Color of the Spectrum
Now let's set the default color of the line. I choose Red as default, you can use whichever color you like.
1 |
|
2 |
if(!changeClr){ |
3 |
lineClr = 0xFF0000; |
4 |
redBtn.alpha = 1; |
5 |
yellowBtn.alpha = 0.5; |
6 |
blueBtn.alpha = 0.5; |
7 |
}
|
Step 33 - Draw the Spectrum
The last part of our Actionscript; we want Flash to draw out the spectrum of the song. "lineClr" in "lineStyle" is a variable within the colour button in step 31. "for(var i:uint=30; i<280; i++)" stated the starting x coordinate of the line and the end coordinate of the line.
In "var num:Number = ba.readFloat()*50 + 170;" the 50 allows Flash to multiply the Float in the song and 170 is the y coordinate of the spectrum.
1 |
|
2 |
var ba:ByteArray = new ByteArray(); |
3 |
addEventListener(Event.ENTER_FRAME, drawl); |
4 |
function drawl(e:Event):void |
5 |
{
|
6 |
graphics.clear(); |
7 |
graphics.lineStyle(1, lineClr, 0.5); |
8 |
graphics.moveTo(30, 150); |
9 |
SoundMixer.computeSpectrum(ba); |
10 |
for(var i:uint=30; i<280; i++) |
11 |
{
|
12 |
var num:Number = ba.readFloat()*50 + 170; |
13 |
graphics.lineTo(i, num); |
14 |
}
|
15 |
}
|
Step 34 - Create the XML
Title and Artist will be shown in our player later. "<thumb>" and "<url>" are the paths of the album art and the song. XML can be created easily (you can use Notepad to type the following code and Save as .XML file). You must save it as "musiclist.xml" as that's how we've referred to it within the Actionscript of the Player.
1 |
|
2 |
<musiclist> |
3 |
|
4 |
<song> |
5 |
<title>The Mass</title> |
6 |
<artist>Era</artist> |
7 |
<thumb>pics/era.jpg</thumb> |
8 |
<url>songs/themass.mp3</url> |
9 |
</song> |
10 |
<song> |
11 |
<title>Dr.No theme</title> |
12 |
<artist> - </artist> |
13 |
<thumb>pics/drno.jpg</thumb> |
14 |
<url>songs/drno.mp3</url> |
15 |
</song> |
16 |
|
17 |
</musiclist> |
Step 35 - Create Album Art
Create album art for the songs. Recommended size is 65px x 65px; it will fit nicely in the screen.


Conclusion
Test your movie (Ctrl + Enter) to check the outcome. You will have your own flash iPod Player! You can use it as music player for your website; edit the music list as you wish. It's up to you. Go ahead and experiment, if you have any cool results be sure to share them with us.
I hope you liked this tutorial, thanks for reading!





