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.
/** * 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 of the XML file nr_txt.text = songlist[0].number; //I load the of the XML file gen_txt.text = songlist[0].gen; //I load the 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 theof the XML file nr_txt.text = songlist[currentIndex].number;//I load theof the XML file gen_txt.text = songlist[currentIndex].gen;//I load the 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
yo, blog.flashdesign-store.com great name for site
Hello, I can’t understand how to add your blog ( blog.flashdesign-store.com ) in my rss reader
Just add http://blog.flashdesign-store.com/?feed=rss2, in you rss reader
Great post. Gives me what I have been looking for.
Yeah…it’s incredible tutorial….Great.
I’m looking forward for your another great tutorial
how about displaying duration of song and current time in your mp3 player
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.
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.
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.
@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.
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.
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
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
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
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
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();
SoundMixer.stopAll(); i added that function in the whenLoaded function but i doesn’t work
Hmm, very interesting. Well then change the -isPlaying- to false, var isPlaying:Boolean = false;.
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
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.
@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.
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.
here is the link for the mp3Player enjoy.
http://rapidshare.com/files/247268799/mp3PlayerCS4.rar.html
NOTE: for flash cs4 only
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,
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.
OK budz, I look forward to see the next player.
Horatiu,
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.
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
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!
Hello David,
Change the -isPlaying- to false, var isPlaying:Boolean = false;.
If doesen’t work, write me.
Horia,
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.
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.
SOURCE FILES PLEASE SAVE MY LIFE
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.
@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.
Thanks that was a excellent post!
thank!
but i can’t understand. struc of the xml file?? can you me???!
Thanks so much for the download because you have saved me a lot of time. God Bless
Very interesting blog. I will come regularly here. Thanks the author
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. . . .
Salut!
Mestere esti cel mai tare! Mi-a placut ideea cu XML-ul. Iti spun Multumesc… si Keep the hard work runnin`