Posted on July 23rd, 2005
106990 views
MP3 player in Flash - No problem
MP3 player in Flash controlled by a XML file - Could be a problem No panic, that's what I will going to show you now. In this tutorial I will just cover the basics of the mp3 player, maybe I will create a more advanced one with some more features. Let's get playing those mp3 ;) - Step 1 First of all we need to create a few buttons, so we can control our player ;) I've create the buttons using the text tool, and with the font Webdings. Create 4 buttons and give them also the same instanceName:
Furtheron, create a dynamic textfield where you will show the songname or whatever we want, give it an instanceName "display_txt" without the quotes Create a new Layer in your movie, rename it to Actions, and put a stop action on frame 1, Actions Layer. - Step 2 Now that we have something to control the player, we need to feed the player with something, we will use XML for that task. Open your favourite texteditor, I use Notepad or Dreamweaver, and create a new XML file. Type following code into that XML file you created: <?xml version='1.0' encoding='utf-8'?> <songs> <song /> </songs> Now you just need to fill the information about your songs between those 2 tags Here what I've done: <?xml version='1.0' encoding='utf-8'?> <songs> <song name=\"Pixel2Life Soundtrack 01\" file=\"music/p2l_01.mp3\" /> <song name=\"Twodded Soundtrack 01\" file=\"music/twod_01.mp3\" /> </songs> Save this newly created XML file as "playlist.xml" in the same directory as your .fla file. - Step 3 Now we need to feed the XML into flash, this will afford a lot of scripting. I will try to explain by my best. First we create a new XML object: playlist = new XML(); Then we say that he should ignore whitespaces playlist.ignoreWhite = true; Then we create 2 global arrays for the playlist, one for the Songtitle and one for the filepath. playlist.onload = function (success) { _global.songname = []; _global.songfile = []; if(success) { for (var i=0; i<playlist.firstChild.childNodes.length; i++) { _global.songname[i] = playlist.firstChild.childNodes[i].attributes.name; _global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file; } } else {display_txt.text=\"Error loading XML\"} } With this Script, I tell Flash: "If playlist loaded successful, create 2 containers and check how many songs are in there, if no XML display an Error. at the end of this script we just need to tell flash to load the xml file we want playlist.load(\"playlist.xml\"); If you want to see if everthing is loaded correctly, just place a trace action after the "_global.songfile". trace(songname[i]+\" \"+songfile[i]); this will display your array in the output window. - Step 4 Now that we have the basic functionallity of the player, let's put the music into it. _root.createEmptyMovieClip(\"sound_mc\",1); Here we create a emptymovieclip that will hold our music. _root.sound_mc.sound_obj = new Sound(); This will place our music inside of our newly created Movieclip _root.sound_mc.songStarter(songfile[0],songname[0]); This bit will load and start our first song in the array. MovieClip.prototype.songStarter= function (file, name) { this.sound_obj.loadSound(file,true) // true = streaming this.onEnterFrame = function () { // onEnter Frame event if(this.sound_obj.position>0) { //When sound starts delete this.onEnterFrame; // delete the event trace(\"Song has started\");//Just checking if everything works } } } Here the complete code we've created: playlist = new XML(); playlist.ignoreWhite=true; playlist.onload = function (success) { if(success) { _global.songname = []; _global.songfile = []; for (var i=0; i<playlist.firstChild.childNodes.length; i++) { _global.songname[i] = playlist.firstChild.childNodes[i].attributes.name; _global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file; trace(songname[i]+\" \"+songfile[i]); } _root.createEmptyMovieClip(\"sound_mc\",1); _root.sound_mc.sound_obj = new Sound(); _root.sound_mc.songStarter(songfile[0],songname[0]); } else {display_txt.text=\"Error loading XML\"} } MovieClip.prototype.songStarter = function (file, name) { this.sound_obj.loadSound(file,true) this.onEnterFrame = function () { if(this.sound_obj.position>0) { delete this.onEnterFrame; this._parent.display_txt.text=name; } else { this._parent.display_txt.text=\"loading...\" } } } playlist.load(\"playlist.xml\"); Now if you test your movie with CTRL+ENTER, you will see that your music is being played, and the songtitle appears on the dynamic textfield you've created. - Step 5 After a few minutes the song will stop (depending how long your track is ;) ) Now you want that the player moves on to the next song. Here what you need to do. This code will be placed inside our Prototype right after the second } that closes our function. this.sound_obj.onSoundComplete = function (){ (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } First line is just telling "when the sound completes..." Second line: "Pick up next song in array.." Third line: "start the next song" are you wondering why I've put a -1? that's because the array starts at 0. - Step 6 Are you still here? If yes, congratulations you almost made it. Now we just need to give our buttons some functionality. PLAYBUTTON: btn_play.onRelease = function () { this._parent.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); STOPBUTTON: btn_stop.onRelease = function() { this._parent.sound_mc.sound_obj.stop(); } NEXTBUTTON: btn_next.onRelease = function () { (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } PREVIOUSBUTTON: btn_prev.onRelease = function () { (song_nr==0)? _global.song_nr=songfile.length-1 : _global.song_nr--; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } COMPLETE CODE stop(); playlist= new XML(); playlist.ignoreWhite=true; playlist.onload = function (success) { if(success) { _global.songname = []; _global.songfile = []; for (var i=0; i<playlist.firstChild.childNodes.length; i++) { _global.songname = playlist.firstChild.childNodes[i].attributes.name; _global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file; trace(songname[i]+\" \"+songfile[i]); } _root.createEmptyMovieClip(\"sound_mc\",1); _root.sound_mc.sound_obj = new Sound(); _global.song_nr = random(songfile.length); _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } else {display_txt.text=\"Error Loading XML\"} } MovieClip.prototype.songStarter = function (file, name) { this.sound_obj.loadSound(file,true) this.onEnterFrame = function () { if(this.sound_obj.position>0) { delete this.onEnterFrame; this._parent.display_txt.text=name; } else { this._parent.display_txt.text=\"loading...\" } } this.sound_obj.onSoundComplete = function () { (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } } btn_play.onRelease = function () { this._parent.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } btn_stop.onRelease = function() { this._parent.sound_mc.sound_obj.stop(); } btn_next.onRelease = function () { (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } btn_prev.onRelease = function () { (song_nr==0)? _global.song_nr=songfile.length-1 : _global.song_nr--; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } playlist.load(\"playlist.xml\"); FINISHED!! Boys & Girls that's it for the moment, now you got your streaming player running being feeded by a XML file. HERE you can download my .zip file with 2 sample mp3's and the working .fla HERE you can also view a Live Example of the player. Have fun with it. Part II starts HERE |