Products
Technologies

Developer resources
Not yet rated

Problem

I need to make a mp3 player in flash builder 4, that has a play pause next previous button with a xml playlis .

Solution

Developing an MP3 Player with Flash/Flex can be done using simple Button components and Actionscript. If you know how to load an MP3 then all the work is really parsing though the XML; In this example we are just going to use a local string as our XML datasource. In a real world example you would want to use URLLoader to load the XML from an external file.

Detailed explanation

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               applicationComplete="init();">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
       
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import flash.events.Event;
            import flash.media.Sound;
            import flash.media.SoundChannel;
            import flash.media.SoundTransform;
            import flash.net.URLRequest;
            import flash.xml.*;
           
            private var _xmlDoc:String;
            private var _xmlObj:XML;
            private var _soundObj:Sound;
            private var _soundTransform:SoundTransform;
            private var _soundChannel:SoundChannel;
           
            private var _currentArrayNum:int;
            private var _currentSndPosition:Number;


            private var _isPlayingSound:Boolean;
           //helps you keep track of if the song is playing or not.
           
           
            private function init():void
            {
                _xmlDoc = '<?xml version="1.0" encoding="utf-8"?><mp3><songURL>mp3/01 - New Life.mp3</songURL></mp3>';
                _xmlObj = new XML(_xmlDoc);
                trace(_xmlObj..songURL);
               //if there were more "songURL" tags then you could treat this like like an array. the first song being in position 0 
               
                _currentArrayNum = 0;
                //this var helps keep track of where you are in the array when you click next or previous.

           


                _soundObj = new Sound();
                _soundObj.load(new URLRequest(_xmlObj..songURL[_currentArrayNum]));
               
                _soundChannel = new SoundChannel();
                _soundTransform = new SoundTransform();
               
                VSlider.snapInterval =.1;
                VSlider.addEventListener(Event.CHANGE, ChangeVolume);

               
               
               
            }
           
            private function playSound():void
            {
                _soundChannel = _soundObj.play();
                _isPlayingSound = true;
                _soundTransform.volume = .5;
                _soundChannel.soundTransform = _soundTransform;               
            }
           
            private function pauseSound():void
            {
                if(_isPlayingSound == true){
                    _currentSndPosition = _soundChannel.position;
                    trace("_currentSndPosition = " + _currentSndPosition);
                    _isPlayingSound = false;
                    _soundChannel.stop();
                }else{
                    _soundChannel = _soundObj.play(_currentSndPosition);
                    _isPlayingSound = true;
                    trace("_currentSndPosition = " + _currentSndPosition);
                }
            }
           
            //if your var's value is not yet the equal fo the highest number in your songURL array. then add 1 and load the next
           //the opposite is true going backwards
            private function nextSound():void
            {
                if(_currentArrayNum != _xmlObj..songURL.length()){
                   
                    _currentArrayNum ++;
                    _soundObj.load(new URLRequest(_xmlObj..songURL[_currentArrayNum]));
                }
            }
           
            private function prevSound():void
            {
                if(_currentArrayNum !=0){
                   
                    _currentArrayNum --;
                    _soundObj.load(new URLRequest(_xmlObj..songURL[_currentArrayNum]));
                }
            }
           

//the slider has a min value of 0 and a max value of 1 and will snap to each position in between (.1)
//what ever that number is in between 0 and 1 is the new volume
            private function ChangeVolume(evt:Event):void{
                _soundTransform.volume = evt.target.value;
                _soundChannel.soundTransform = _soundTransform;
                trace(evt.target.value);
            }
        ]]>
    </fx:Script>


<s:Group>
    <s:layout>
        <s:HorizontalLayout/>
    </s:layout>
   
    <s:Button label="Play" click="playSound();"/>
    <s:Button label="Pause" click="pauseSound();"/>
    <s:Button label="Next" click="nextSound();"/>
    <s:Button label="Previous" click="prevSound()"/>
    <s:HSlider id="VSlider" minimum="0" maximum="1" />
</s:Group>
   
</s:Application>


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes