
List Component –
| import fl.controls.List; var xmlLoader:URLLoader = new URLLoader(); var xmlData:XML = new XML(); xmlLoader.load(new URLRequest("songs.xml")); xmlLoader.addEventListener(Event.COMPLETE, LoadXML); function LoadXML(e:Event):void { xmlData = new XML(e.target.data); Parsemusic(xmlData); } function Parsemusic(musicInput:XML):void { var musicname:XMLList =musicInput.tracks.mname; var musicurl:XMLList = musicInput.tracks.murl; for (var i:int = 0; i < musicname.length(); i++) { var nameElement:XML = musicname[i]; var urlElement:XML = musicurl[i]; tracklist.addItem({label:musicname[i], data:musicurl[i]}); } } |
Next step is to get the player up and running. To accomplish the above, we will have to provide an EventListerner to our List Componenet ‘tracklist’ to check for song selection, grab the song url / data from tracklist, check if the song is loaded, or if there is an error while loading and to play. There is another very important thing which I HAVE TO mention. As you all are aware, to play a song, we will have to create a ‘Sound Object’ and allot play method to Sound Object for playing select music, but the problem here is, we cannot just finish with this, because there is a ‘Memory leak’ problem with AS3. To overcome this problem, we will have to create a ‘player object’ and then allot sound object to the same.
Functionalities for buttons and sliders are also included
| var sc:SoundChannel=new SoundChannel(); var flagsc:Number=0; var songurl:URLRequest = new URLRequest(); var playerObject:Object = new Object(); var pausePoint:Number = 0.00; var isPlaying:Boolean; var muted:Boolean = false; var soundvolume:Number = 1; var forwardDuration:Number = 5000; var fileTimeInSec:Number; tracklist.addEventListener(Event.CHANGE, tracklistchange); function tracklistchange(event:Event) { if (flagsc==1) { sc.stop(); } else if (flagsc==0) { flagsc=1; } playSound(event.target.selectedItem.data); } function playSound(url) { songurl.url = url; if (playerObject.sound!=null) { delete playerObject.sound; } playerObject.sound = new Sound(); soundevents(); playerObject.sound.load(songurl); } //sound events for 'complete', 'error' and 'progress' function soundevents():void { playerObject.sound.addEventListener(Event.COMPLETE, musicloaded); playerObject.sound.addEventListener(IOErrorEvent.IO_ERROR, onIOError); playerObject.sound.addEventListener(ProgressEvent.PROGRESS,funcOnLoadProgress); } function musicloaded(event:Event) { sc=playerObject.sound.play(); isPlaying = true; play_btn.visible = false; pause_btn.visible = true; } function funcOnLoadProgress(event:ProgressEvent):void { var loadedPct:uint =Math.round(100 * (event.bytesLoaded / event.bytesTotal)); trace("The playerObject.sound is " + loadedPct + "% loaded."); } function onIOError(e:IOErrorEvent) { trace("The playerObject.sound can not be Loaded"+ e.text); } //btns play_btn.addEventListener(MouseEvent.CLICK,playmusic); stop_btn.addEventListener(MouseEvent.CLICK,stopmusic); pause_btn.addEventListener(MouseEvent.CLICK,pausemusic); back_btn.addEventListener(MouseEvent.CLICK,backmusic); fwd_btn.addEventListener(MouseEvent.CLICK,fwdmusic); mute_btn.addEventListener(MouseEvent.CLICK,mute); //play button function playmusic(event:Event) { if (!isPlaying) { sc = playerObject.sound.play(pausePoint); isPlaying = true; play_btn.visible = false; pause_btn.visible = true; } } //pause button function pausemusic(event:Event) { if (isPlaying) { pausePoint = sc.position; sc.stop(); isPlaying = false; play_btn.visible = true; pause_btn.visible = false; } } //stop button function stopmusic(event:Event):void { if (isPlaying) { sc.stop(); isPlaying = false; play_btn.visible = true; pause_btn.visible = false; } pausePoint = 0.00; flagsc=0; } //mute button function mute(event:Event) { if (sc!=null) { var st:SoundTransform; if (muted) { st = new SoundTransform(soundvolume); sc.soundTransform = st; this.mute_btn.on_mc.visible = true; muted = false; } else { st = new SoundTransform(0); sc.soundTransform = st; this.mute_btn.on_mc.visible = false; muted = true; } } } function fwdmusic(event:Event) { if (sc.position+forwardDuration<=playerObject.sound.bytesTotal) { sc.stop(); sc=playerObject.sound.play(sc.position+forwardDuration); } else { pausePoint=0; sc.stop(); flagsc=0; } } function backmusic(event:Event) { if (sc.position-forwardDuration>0) { sc.stop(); sc=playerObject.sound.play(sc.position-forwardDuration); } else { pausePoint=0; sc.stop(); flagsc=0; } } var rect:Rectangle = new Rectangle(0,-5,100,0); volume_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, slideit); stage.addEventListener(MouseEvent.MOUSE_UP, stopslide); function slideit(event:Event) { volume_mc.slider_mc.startDrag(false, rect); addEventListener(Event.ENTER_FRAME, volumecontrol); } function stopslide(event:Event) { if(dragging) { volume_mc.slider_mc.stopDrag(); } } function volumecontrol(event:Event) { var val:Number = volume_mc.slider_mc.x/100; var st:SoundTransform = new SoundTransform(val); if(sc!=null) { sc.soundTransform = st; muted = false; } } |
I hope the explanation and script was simpler to understand.
Click here to download the source code
- Downloadable source code file also includes script for slider.
The slider didn't seem to work properly over the net so I removed it from this tutorial
and thought of retaining the same in source code file, just in case, if you need
it.
Tags: Flash MP3 player, Flash audio player, List Component, Loading external XML data to list component, Sound, Sound Channel and Sound Transform class