AS 3.0 XML MP3 Player
February 2nd, 2009 Posted in ActionScript 3.0
Hey, I came back with something new and interesting, I make a Mp3 Player. Is not a very advenced but is not so easy to understud if you are new in this. Here is the code, and if you whant to ask me something leave me a comment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | /** * Author: Horatiu Condrea ( http://www.flashdesign-store.com ) * Description: AS 3.0 MP3 Player **/ // ------------------------------------- // Imports // ------------------------------------- //I import the TweenMax class because, she helps me to make same tween in the stage import gs.TweenMax; import gs.easing.*; stage.scaleMode = StageScaleMode.NO_SCALE; // ------------------------------------- // Variables // ------------------------------------- var sound:Sound = new Sound(); var musicReq:URLRequest; //The BytesArray helps me to "see" the sound bytes and the transform to a spectrum var ba:ByteArray = new ByteArray(); var buffer:SoundLoaderContext; var sc:SoundChannel; var st:SoundTransform; var isPlaying:Boolean = true; var pos:Number; var currentSound:Sound = sound; var xml:XML; var songlist:XMLList; var currentIndex:Number = 0; var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE, whenLoaded); //Here we load in our XML and create a function that will run when our loader has completed loading function whenLoaded(e:Event):void { xml = new XML(e.target.data); songlist = xml.song; musicReq = new URLRequest(songlist[0].url); sound.load(musicReq); sc = sound.play(); title_txt.text = songlist[0].title; //I load the <title> of the XML file nr_txt.text = songlist[0].number; //I load the <number> of the XML file gen_txt.text = songlist[0].gen; //I load the <gen> of the XML file } //My XML file name is "main.xml" and in this part I load the XML loader.load(new URLRequest("main.xml")); // I created these 2 variables(bmd and bm) // Because the filter must know where to act var bmd:BitmapData = new BitmapData(512, 300, true, 0x000000); var bm:Bitmap = new Bitmap(bmd); addChild(bm); //I create a new Sprite for the spectrum line var sp:Sprite = new Sprite(); addChild(sp); var blur:BlurFilter = new BlurFilter(8,8,2); // Set the BlurFilter Effect var colorMatrix:ColorMatrixFilter = new ColorMatrixFilter ([1 ,0, 0, 0, 0, 0, 1, 0, 0, 0, // Set the ColorMatrixFilter Effect 0, 0, 1, 0, 0, 0, 0, 0, 0.96, 0]); // ------------------------------------- // Stage Objects // ------------------------------------- //Well in this part I put the objects on the stage,my object are Exporting to ActionScript, and the objects are in my library. //Some mathematical stufe var bar_a = new Bar_a(); addChild(bar_a); bar_a.y = stage.stageHeight * 0.5 - 25; bar_a.x = stage.stageWidth * 0.5; var bar_b = new Bar_b(); addChild(bar_b); bar_b.y = stage.stageHeight * 0.5 + 25; bar_b.x = stage.stageWidth * 0.5; var play_btn = new Play(); addChild(play_btn); play_btn.y = stage.stageHeight * 0.5 - 15; play_btn.x = 80; play_btn.scaleX = 1.2; play_btn.scaleY = 1.2; var stop_btn = new Stop(); addChild(stop_btn); stop_btn.y = stage.stageHeight * 0.5 - 15; stop_btn.x = 40; stop_btn.scaleX = 1.2; stop_btn.scaleY = 1.2; var pause_btn = new Pause(); addChild(pause_btn); pause_btn.y = stage.stageHeight * 0.5 - 15; pause_btn.x = 60; pause_btn.scaleX = 1.2; pause_btn.scaleY = 1.2; var next_btn = new Next(); addChild(next_btn); next_btn.y = stage.stageHeight * 0.5 - 15; next_btn.x = 100; next_btn.scaleX = 1.2; next_btn.scaleY = 1.2; var prev_btn = new Prev(); addChild(prev_btn); prev_btn.y = stage.stageHeight * 0.5 - 11; prev_btn.x = 20; prev_btn.scaleX = 1.2; prev_btn.scaleY = 1.2; var volume_mc = new Volume(); addChild(volume_mc); volume_mc.y = stage.stageHeight * 0.5 + 10; volume_mc.x = 325; buffer = new SoundLoaderContext(5000) // ------------------------------------- // Buttons & Function // ------------------------------------- //Now we'll add some Mouse Events for our buttons on the MP3 Player and give them some functions stop_btn.addEventListener(MouseEvent.CLICK, stopSound); function stopSound(e:MouseEvent):void { SoundMixer.stopAll(); //With this function I stop all the music isPlaying = false; } pause_btn.addEventListener(MouseEvent.CLICK, pauseSound); function pauseSound(e:MouseEvent):void { //Well, if you look over the variables, I will find the "pos:number" variable, this will take the muisc position and then I stope the music because is a pause_btn pos = sc.position; sc.stop(); isPlaying = false; } play_btn.addEventListener(MouseEvent.CLICK, playSound); function playSound(e:MouseEvent):void { //Here I start the muzic, but I start the muisc in the "pos" position if(isPlaying == false) { sc = currentSound.play(pos); isPlaying = true; } } next_btn.addEventListener(MouseEvent.CLICK, nextSong); function nextSong(e:Event):void //Well in this function I start the next song, if you are clicking on the next_btn { if (currentIndex < (songlist.length() + 1)) { currentIndex++; } else { currentIndex = 0; } var nextReq:URLRequest = new URLRequest(songlist[currentIndex].url); var nextTitle:Sound = new Sound(nextReq); sc.stop(); title_txt.text = songlist[currentIndex].title;//I load the<title>of the XML file nr_txt.text = songlist[currentIndex].number;//I load the<number>of the XML file gen_txt.text = songlist[currentIndex].gen;//I load the <gen> of the XML file sc = nextTitle.play(); isPlaying = true; currentSound = nextTitle; } prev_btn.addEventListener(MouseEvent.CLICK, prevSong); function prevSong(e:Event):void //Here I start the previos song { if (currentIndex < (songlist.length() - 1)) { currentIndex++; } else { currentIndex = 0; } var nextReq:URLRequest = new URLRequest(songlist[currentIndex].url); var nextTitle:Sound = new Sound(nextReq); sc.stop(); title_txt.text = songlist[currentIndex].title; nr_txt.text = songlist[currentIndex].number;//Again load the number, gen and title of the XML file gen_txt.text = songlist[currentIndex].gen; sc = nextTitle.play(); isPlaying = true; currentSound = nextTitle; } // ------------------------------------- // Spectrums // ------------------------------------- addEventListener(Event.ENTER_FRAME, loop_a); addEventListener(Event.ENTER_FRAME, loop_b); function loop_a(e:Event):void //Well here is the first spectrum { sp.graphics.clear(); sp.graphics.lineStyle(2, 0xfffff0);//Select a color and a thickness for the line spectrum sp.graphics.moveTo(-5, 50); //I put my target for line SoundMixer.computeSpectrum(ba); for(var i:uint=0; i<256; i++) { var num:Number = -ba.readFloat()*50 + 50; sp.graphics.lineTo(i*2, num); } bmd.draw(sp); // Apply the filter effects to line bmd.applyFilter(bmd,bmd.rect,new Point(),blur); bmd.applyFilter(bmd,bmd.rect,new Point(),colorMatrix); bmd.scroll(3,0); } function loop_b(e:Event):void //Here is the second spectrum, and it is make with the same way { sp.graphics.clear(); sp.graphics.lineStyle(2, 0xfffff0); sp.graphics.moveTo(-1, 250); SoundMixer.computeSpectrum(ba); for(var i:uint=0; i<256; i++) { var num:Number = -ba.readFloat()*50 + 250; sp.graphics.lineTo(i*2, num); } bmd.draw(sp); bmd.applyFilter(bmd,bmd.rect,new Point(),blur); bmd.applyFilter(bmd,bmd.rect,new Point(),colorMatrix); bmd.scroll(3,0); } // ------------------------------------- // Progrss Bar // ------------------------------------- progress_mc.addEventListener(MouseEvent.CLICK, setNewProgress); addEventListener(Event.ENTER_FRAME, calcProgress); function calcProgress(e:Event):void //In this function I caluclate the music progress for the Progrss Bar { var p:MovieClip = this.progress_mc.mcProgressFill; var w:uint = Math.round( 175 * sc.position / currentSound.length); p.width = w; } function setNewProgress(e:MouseEvent):void { var t:uint = currentSound.length * e.currentTarget.mouseX / 175; sc.stop(); sc = currentSound.play(t); } // ------------------------------------- // Volume // ------------------------------------- var drag:Boolean = false; var rectangle:Rectangle = new Rectangle(0,0,150,0);//I create a new rectangle volume_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragIt); stage.addEventListener(MouseEvent.MOUSE_UP, dropIt); volume_mc.slider_mc.buttonMode = true; function dragIt(e:Event):void { volume_mc.slider_mc.startDrag(false,rectangle); drag = true; volume_mc.slider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume); } function adjustVolume(e:Event):void { var vol:Number = volume_mc.slider_mc.x / 150; var st:SoundTransform = new SoundTransform(vol); if (sc != null) { sc.soundTransform = st; } } function dropIt(e:Event):void { if (drag) { volume_mc.slider_mc.stopDrag(); drag = false; } } // ------------------------------------- // Buttons Effects // ------------------------------------- //Well, here is going to helps me the TweenMax, Here is runing the same effect, but it is put in the all buttons play_btn.addEventListener(MouseEvent.ROLL_OVER, rollover) function rollover(e:MouseEvent):void { TweenMax.to(play_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}}); } play_btn.addEventListener(MouseEvent.ROLL_OUT, rollout) function rollout(e:MouseEvent):void { TweenMax.to(play_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}}); } stop_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_b) function rollover_b(e:MouseEvent):void { TweenMax.to(stop_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}}); } stop_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_b) function rollout_b(e:MouseEvent):void { TweenMax.to(stop_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}}); } pause_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_c) function rollover_c(e:MouseEvent):void { TweenMax.to(pause_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}}); } pause_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_c) function rollout_c(e:MouseEvent):void { TweenMax.to(pause_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}}); } next_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_d) function rollover_d(e:MouseEvent):void { TweenMax.to(next_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}}); } next_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_d) function rollout_d(e:MouseEvent):void { TweenMax.to(next_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}}); } prev_btn.addEventListener(MouseEvent.ROLL_OVER, rollover_e) function rollover_e(e:MouseEvent):void { TweenMax.to(prev_btn, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}}); } prev_btn.addEventListener(MouseEvent.ROLL_OUT, rollout_e) function rollout_e(e:MouseEvent):void { TweenMax.to(prev_btn, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}}); } volume_mc.slider_mc.addEventListener(MouseEvent.ROLL_OVER, rollover_f) function rollover_f(e:MouseEvent):void { TweenMax.to(volume_mc.slider_mc, 1, {colorMatrixFilter:{colorize:0x666666, amount:1}}); } volume_mc.slider_mc.addEventListener(MouseEvent.ROLL_OUT, rollout_f) function rollout_f(e:MouseEvent):void { TweenMax.to(volume_mc.slider_mc, 1, {colorMatrixFilter:{colorize:0xffffff, amount:1}}); } stop(); |
Well don’t worry is not so hard to make it, but is more to write.
Here is the Mp3 Player:AS 3.0 Mp3 Player

February 14th, 2009 at 8:26 AM
yo, blog.flashdesign-store.com great name for site
February 14th, 2009 at 9:17 PM
Hello, I can’t understand how to add your blog ( blog.flashdesign-store.com ) in my rss reader
March 17th, 2009 at 10:33 PM
Just add http://blog.flashdesign-store.com/?feed=rss2, in you rss reader
March 26th, 2009 at 4:27 PM
Great post. Gives me what I have been looking for.
April 3rd, 2009 at 10:46 AM
Yeah…it’s incredible tutorial….Great.
I’m looking forward for your another great tutorial
April 3rd, 2009 at 10:49 AM
how about displaying duration of song and current time in your mp3 player
April 3rd, 2009 at 1:04 PM
Obiez Devil, thanks for the compliments, is not a bad idea, but now I have much to work, and I don’t have much time. But in the future I will make an update to the MP3 Player.
May 14th, 2009 at 5:06 PM
could you pls. fix this error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_fla::MainTimeline/calcProgress()
anyway thank you for this tutorial.
I hope this error #1009 should be fix asap.
May 14th, 2009 at 5:16 PM
Well, this error is because the progress bar, is in the Library, and have the instance name “progress_mc”. Just draw a line on the stage, and named progress_mc in this Movie CLip draw a litle line caled “mcProgressFill” This is the line that will make large in the same time with song.
May 15th, 2009 at 2:21 AM
@Horatiu Condrea
same as the above mentioned by ken Error #1009: Cannot access a property or method of a null object reference.
at Untitled_fla::MainTimeline/calcProgress()
and I did already, I draw a rectangle and name it progress_mc and inside the movie clip I also draw a rectangle in name it mcProgressFill but the error #1009 still exist.
May 15th, 2009 at 8:27 AM
hmm…very interesting, if you really want this mp3 player, I will send you the original work without any error. I can’t realize what is the problem with this error.
May 23rd, 2009 at 3:37 AM
Hey i really like your player do u have a tutorial of this somewhere so i can make it and use it or could you please give me a download link
thanks
May 23rd, 2009 at 8:48 AM
hey i have tried to make this player but i can’t seem to be able to. i am getting about 14 errors and most of them are about import gs.TweenMax
for some reason it doesn’t import it
i really like this tutorial do you reckon you could give me a download link
May 23rd, 2009 at 9:59 AM
Usually I don’t do this, because I want to put this player on FlashDen. But because many people like this player. I will gave you a download link, and I put a donation system, if you realy like this player, you can make a donation.
Download link: CLICK
May 23rd, 2009 at 11:18 AM
Thanx for the download link but i have 2 question
How do i make the stop button stop the song but also start it from the beginning and also how do i stop the music playing when i enter swf and make it play when i click play
May 23rd, 2009 at 12:07 PM
Well, I will give you the easy whay to do this:
1. Move the play button at the same position as the stop button and when the -isPlaying = true;- the stop_btn is visible, and play_btn is invisible… and when the -isPlaying = false;- the play_btn is visible and the stop_btn is invisible. Remember before to do this one button must be invisible, this depends if the music is playing or not when you start the swf file.
2. Just add this line of code in the -whenLoaded- function
SoundMixer.stopAll();
May 23rd, 2009 at 2:39 PM
SoundMixer.stopAll(); i added that function in the whenLoaded function but i doesn’t work
May 24th, 2009 at 10:43 AM
Hmm, very interesting. Well then change the -isPlaying- to false, var isPlaying:Boolean = false;.
May 27th, 2009 at 9:30 PM
Thank you very much for this player mate, i downloaded the player and when i loaded a new mp3 file that is larger that yours it gives me Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
at index_fla::MainTimeline/nextSong()
What is this exactly ?
Thanks in advance mate
May 28th, 2009 at 9:57 AM
Well, if you have 2 ore more songs add on this player, when you arrive on the last song, and you want to go to the first song again this error appears, actually is not an error, it’s just an warring.
Soon I will make a new MP3 Player and I will posted on the blog, If you wait, you cand downloaded that one.
June 19th, 2009 at 8:29 AM
@ShadowRunner
add this line of code sc.addEventListener(Event.SOUND_COMPLETE, nextSong); for next,previous buttons & Progress bar. last line of setNewProgress also the last line for next & prev buttons.
June 22nd, 2009 at 9:27 AM
Hi everyone,
In respect to the author of this mp3Player AS3. I reconstructed the codes for this mp3Player a week ago now and put additional features.
This tutorial is very helpful especially to a newbie like me. But I make this more editable.I re-skinned the player and changed the buttons with my own style. I created a mute button, volume percent display, playback percent display, and progress time display and replace the compute spectrum with my own style. So if you interested the player I’d done? I’ll give you the link where you can download the player [ASAP] in exchange with a simple thank you. lol…Thanks to the author and it’s really nice player.I really appreciated it. Thank you and sorry for my long comments.
June 22nd, 2009 at 10:03 AM
here is the link for the mp3Player enjoy.
http://rapidshare.com/files/247268799/mp3PlayerCS4.rar.html
NOTE: for flash cs4 only
June 22nd, 2009 at 1:09 PM
Hi Budz, and I’m glad that you liked my player, and is an honor to see that you did a player after my. Now I’m are very very busy, but I hope to see you on the following posts with comments.
Cya next time…
Horatiu,
July 21st, 2009 at 5:39 PM
hehehe…very interesting,I have found some bugs in my recently posted flash mp3 player. so, I have decided to delete the link that I’m have given you. sorry for that, but I will fix it asap. I will post another link again for cs3 & cs4. with album art and balance control. The link above is now close and not available anymore. thank you.
July 21st, 2009 at 6:20 PM
OK budz, I look forward to see the next player.
Horatiu,
July 26th, 2009 at 3:44 PM
here is the new link for mp3 player as3 cs3 & cs4.
http://rapidshare.com/files/260229906/myMP3.exe
additional features:
album art.
play indicator.
balance control.
also with the time display and playback percent.
Thank you.
July 26th, 2009 at 4:03 PM
note:
double click to extract the myMp3.exe or rename it to myMp3.rar
This player is more features, than the other one that i posted earlier.
Thanks to horatiu cordrea for making this flash mp3 player tutorial.
All you have to explore the codes to make it more features.
Now its your turn to create a selectable playlist. that’s it for now.
Thank you & enjoy
September 27th, 2009 at 6:15 AM
Hi. Awesome tutorial and great mp3 player…thank you! I have a question, though. I’m very new to Flash and I was wondering how to disable autoplay. How can I adjust the code to prevent the player from starting right away? Any response is greatly appreciated. Great blog!
September 27th, 2009 at 12:13 PM
Hello David,
Change the -isPlaying- to false, var isPlaying:Boolean = false;.
If doesen’t work, write me.
Horia,
September 28th, 2009 at 3:38 PM
Hi Horatiu. Thanks for your fast reply. I changed the code on line 28 from [var isPlaying:Boolean = true;] to [var isPlaying:Boolean = false;] but the player still starts up automatically in test mode. I tried adjusting a few other lines I thought may be associated with autoplay (177, 202, 226) but no luck.
Any advice you could offer would be greatly appreciated. Thanks.
October 5th, 2009 at 6:10 PM
Sorry David, this player seems to have a problem…(if you want to disable autoplay, I need to change more things). If you want you can download budz’s player, in that player music don’t start automatically. Sorry again is my fault, but now I don’t have time to make another player.
October 14th, 2009 at 11:09 PM
SOURCE FILES PLEASE SAVE MY LIFE
October 15th, 2009 at 8:36 AM
Thanks for interest cristobal
Usually I don’t do this, because I want to put this player on FlashDen. But because many people like this player. I will gave you a download link, and I put a donation system, if you realy like this player, you can make a donation.
Download link: CLICK
This was my 14 comment.
October 24th, 2009 at 10:18 AM
@David
go to line 48 and remove this: “sc = sound.play();”
and add this line of code: “play_btn.addEventListener(MouseEvent.CLICK, playSound);”
simple as that.
January 10th, 2010 at 6:23 AM
Thanks that was a excellent post!
January 22nd, 2010 at 11:47 AM
thank!
but i can’t understand. struc of the xml file?? can you me???!
January 23rd, 2010 at 8:53 PM
Thanks so much for the download because you have saved me a lot of time. God Bless
February 15th, 2010 at 7:42 AM
Very interesting blog. I will come regularly here. Thanks the author
May 17th, 2010 at 7:09 AM
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon. . . .
May 25th, 2010 at 10:14 AM
Salut!
Mestere esti cel mai tare! Mi-a placut ideea cu XML-ul. Iti spun Multumesc… si Keep the hard work runnin`