MP3 PLAYER WITH EQUALIZER AND XML SONG LIST as3 , xml |
This first section of actionscript loads all the songs addressed in the xml file along with their names and artists. Please note that the
name of song must match the actual mp3 file name while the artist
name doesnt.Each of these songs is then added to the list component and is labled by number, name of the song and artist and then selects the first song.
Select the second empty frame of layer one and insert the next part of script.
songURL = new URLRequest("mp3_files/" + trackToPlay + ".mp3");
This part loads the new song after it is clicked on the list component. Now select the last empty frame and insert the last portion of actionscript.
stop(); var snd:Sound = new Sound(); var channel:SoundChannel; var context:SoundLoaderContext = new SoundLoaderContext(5000, true); snd.load(songURL, context); channel = snd.play(pausePosition); list.addEventListener(Event.CHANGE, itemClick); function itemClick (event:Event):void { channel.stop(); // stop play Rtext.status_txt.text = "Now Playing: " + event.target.selectedItem.label + ".mp3"; trackToPlay = event.target.selectedItem.songString; Rtext.gotoAndPlay(1); gotoAndPlay(2); } stop_btn.addEventListener(MouseEvent.CLICK, stopPlayer); function stopPlayer (event:MouseEvent):void { channel.stop(); Rtext.gotoAndStop(240); } channel.addEventListener(Event.SOUND_COMPLETE, onCompletePlayback); function onCompletePlayback (event:Event):void { newTrack(); } function newTrack():void { if (list.selectedItem.songNum == i) { channel.stop(); var selectFirst = new Array (0,0); list.selectedIndices = selectFirst; list.scrollToIndex(0); trackToPlay = list.selectedItem.songString; Rtext.status_txt.text = "Now Playing: " + list.selectedItem.label + ".mp3"; gotoAndPlay(2); } else { channel.stop(); var sn:uint = list.selectedItem.songNum; var selectNext = new Array (sn,sn); list.selectedIndices = selectNext; list.scrollToIndex(sn); trackToPlay = list.selectedItem.songString; Rtext.status_txt.text = "Now Playing: " + list.selectedItem.label + ".mp3"; gotoAndPlay(2); } pausePosition = 0; } addEventListener(Event.ENTER_FRAME, onEnterFrame); function onEnterFrame(event:Event):void { eqBarLeft1.gotoAndStop (Math.round(channel.leftPeak * 10) ); eqBarRight1.gotoAndStop (Math.round(channel.rightPeak * 8) ); eqBarLeft2.gotoAndStop (Math.round(channel.leftPeak * 7) ); eqBarRight2.gotoAndStop (Math.round(channel.rightPeak * 7) ); eqBarLeft3.gotoAndStop (Math.round(channel.leftPeak * 8) ); eqBarRight3.gotoAndStop (Math.round(channel.rightPeak * 10) ); }
Lines 3-7 defines new sound object and assign it to new sound channel. Lines 9-17 creates event listener function. If list with all the songs in clicked,
it changes currently playing song to the new selected one. Lines 20-24 is function for stop button. It stops the playing sound and set the Rtext field to empty frame.
On lines 28-51 is function that checks if song has finished and loads the next following mp3 file and also changes the text accordingly. The last section of script animates
the equalizer bars depending on the sound peaks from left and right sound channel. The final part of this project is the xml file. As I said earlier the song names must match the names of mp3 files in mp3_files folder. The artist names are optional.
The above code represent one song in mp3 player. By duplicating lines 4-7 between xml tags you can add additional songs to the list. Simply create new xml file and copy
this code in. Alternatively all the required files are in source file rar, including mp3 songs used in demo on the previous page.
|