Home / Tutorials / ActionScript / MP3 player made simple with AS3 /

Flash Tutorials

MP3 player made simple with AS3 - Page 1

Posted by : framer on Apr 10, 2008

 

5.0/5

In this flash actionscript tutorial I will show you how to make a simple online flash mp3 player, actually using these techniques you will be able to advance it easily with more features to make a full functional online flash mp3 player.

First we will prepare the stage, eg. all the movieclip instances.

So first make two buttons for the play and stop functionality, then an up arrow and down arrow, right click then separately and convert them to movieclips (not buttons symbols).

In the properties panel give all the movie clips an instance name, like I did. Remember flash does not ignore capital letters so remember where you use upper case letters.

    play button = "playButton_"
    stop button = "stopButton_"
    vol up button = "soundUp_btn"
    vol down button = "soundDown_btn"

Now we are ready for the flash actionscript coding, I have made all the description inline with the code, starting with "//" as comments in flash actionscript so you can just copy and paste all the code.

    // First we define a variable of type URLRequest and attach our mp3 file.
    var soundRequest:URLRequest = new URLRequest("JeffWofford_Trouble.mp3");

    // Then we make a new sound instance and naming it sound_
    var sound_:Sound = new Sound();

    // Also a sound control to control and adjust later on.
    var soundControl:SoundChannel = new SoundChannel();

    // last variable is a soundtransform instance, this is to control the volume up and down function. 
    //(and we set them to 1,1 thats the right and left speakers).
    var transform1:SoundTransform = new SoundTransform(1, 1);

    // now we attach the sound file to the sound_ instance we made before.
    sound_.load(soundRequest);

    // here is four eventlisteners, the control all our buttons, and "listen" and calls functions if the buttons are clicked.
    playButton_.addEventListener(MouseEvent.CLICK, playSound);

    stopButton_.addEventListener(MouseEvent.CLICK, stopSound);

    soundUp_btn.addEventListener(MouseEvent.CLICK, soundUp);

    soundDown_btn.addEventListener(MouseEvent.CLICK, soundDown);

    // This is the function to control the play button, only thing happening is that we attach the soundcontrol 
    //to the sound and calls its play method.

    function playSound(event:MouseEvent):void
    {
      soundControl = sound_.play();
    } 

    // the stop function also uses the coundControl to stop the mp3 file from playing.
    function stopSound(event:MouseEvent):void
    {
      soundControl.stop();
    }

    // Now this is a bit more tricky (not much but more then just stop and play).
    // At the top of our code we declared a soundtranform named transform1
    // this is a value, and by saying ++ we add one to the original volume number then attach it to the soundcontrol.soundTransform

    function soundUp(event:MouseEvent):void
    {
      transform1.volume += .1;
      soundControl.soundTransform = transform1;
    }

    // this is the same as above, we just use -- to subtract from the volume number.
    function soundDown(event:MouseEvent):void
    {
      transform1.volume -= .1;
      soundControl.soundTransform = transform1;
    }

As you might see its quite easy, and now that we have made a simple player, adding a mute and pause button is just as easy, we could even make a volume pan and drag it to control the volume, just set it to the volume value, but that will be in another tutorials.

I hope you learned something, thats all for now.

no comment

Add comment

Please login to post comments.


SUPPORTERS