• home
  • forum
  • my
  • kt
  • download
  • Home / 2D Graphics / Flash / Drawing /

    Photoshop speaker badge symbol

    Author: 2008-10-10 08:17:05 From:

    Hi guys, im back with a new flash actionscript 3.0 tutorial, this time we will be making a mp3 player with a volume slider, I must admit, I tried making one some month ago, it didnt work out that well, but I promis you, this works great so just follow along or download the source code at the bottom.

    Before we get all excited and start working with actionscript, first we need to do some preperations, make a few buttons and graphics to make our mp3 player look good. So try to follow along, you can make your own graphic buttons, so I wont guide you through how I made mine, only the structure.

    So on the visual part, lets make a play, pause and stop button, convert them all into movie clips and give them the following instance names by going to the properties panel. play_btn, pause_btn and stop_btn.

     

     

     

    Now we need to make the volume slider, the volume slider consist of a stroke 100 px wide, (its important that its excacly 100 px). Convert it into a movie clip and name it volume_mc. Inside this volume slider movie clip make a handle graphic, convert it into a movie clip and name it mySlider_mc, and place it at the right end of the stroke like below.

     

    Now we are ready to do some actionscript, so go to the main stage and hit f9 or what ever you do to open up the actionscript panel.

    To make things easier for both you and me I commented the code inline with the actionscripting, so everything should be explained line by line, and you should be able to copy and past the code directly into your own project.

     

    And here is the source code:

    view plain?
    1. //attatch/import the music file.   
    2. var musicPiece:Sound = new Sound(new URLRequest _    
    3. ("http://blog.0tutor.com/JeffWofford_Trouble.mp3"));   
    4.   
    5. // Make a sound channel to change sound configurations.   
    6. var mySoundChannel:SoundChannel;   
    7.   
    8. // This variable is checking if the music is playing   
    9. var isPlaying:Boolean = false;   
    10.   
    11. // to remember the position of the music playing when we pause, _    
    12. to start at the same place   
    13.   
    14.   
    15. var pos:Number = 0;   
    16.   
    17. // First button we will make is a play button, add an eventlistener.   
    18. play_btn.addEventListener(MouseEvent.CLICK, play_);   
    19.   
    20. function play_(event:Event):void {   
    21. //Check if the music is NOT playing, then make it start.   
    22. if (!isPlaying) {   
    23. mySoundChannel = musicPiece.play(pos);   
    24. isPlaying = true;   
    25. }   
    26. }   
    27.   
    28. // The pause button, here we will use the pos variable we made earlier.   
    29. pause_btn.addEventListener(MouseEvent.CLICK, pause_);   
    30.   
    31. function pause_(event:Event):void {   
    32. // if the music is player, save the current time and stop the playing.   
    33. if (isPlaying) {   
    34. pos = mySoundChannel.position;   
    35. mySoundChannel.stop();   
    36. isPlaying = false;   
    37. }   
    38. }   
    39.   
    40. // Now the final button is the stop button, allmost thet same as pause   
    41. stop_btn.addEventListener(MouseEvent.CLICK, stop_);   
    42.   
    43. function stop_(event:Event):void {   
    44. // First we check if there is anything to stop then make it stop, and reset the pos variable to 0 (start time)   
    45. if (mySoundChannel != null) {   
    46. mySoundChannel.stop();   
    47. pos = 0;   
    48. isPlaying = false;   
    49. }   
    50. }   
    51.   
    52. // this is a bit complicated, but I will try to explain anyway.   
    53. // first we make a rectangle and set its width to 100.   
    54. var rectangle:Rectangle = new Rectangle(0,0,100,0);   
    55.   
    56. // then we need a varible to check if the handle is being dragged.   
    57. var dragging:Boolean = false;   
    58.   
    59. // the eventlistener for startdragging   
    60. volume_mc.mySlider_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);   
    61. function startDragging(event:Event):void {   
    62.   
    63. // here we tell flash the margin on where it should be able to drag, (remember 100 pixels back and forth)   
    64. volume_mc.mySlider_mc.startDrag(false,rectangle);   
    65. dragging = true;   
    66.   
    67. // This is very important, an eventlistener so we can change the volume, not only make it look good.   
    68. volume_mc.mySlider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume);   
    69. }   
    70.   
    71. // well here is the adjust volume function, its not that complicated   
    72. function adjustVolume(event:Event):void {   
    73.   
    74. // we make a variable to calculate the volume from the slider handle position and divide it by 100   
    75. var myVol:Number = volume_mc.mySlider_mc.x / 100;   
    76. // then we set it with the mySoundTransform   
    77. var mySoundTransform:SoundTransform = new SoundTransform(myVol);   
    78. if (mySoundChannel != null) {   
    79. mySoundChannel.soundTransform = mySoundTransform;   
    80. }   
    81. }   
    82.   
    83. // and of cause the stop draggin function, I dont feel the need to explain it.   
    84. stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);   
    85. function stopDragging(event:Event):void {   
    86. if (dragging) {   
    87. dragging = false;   
    88. volume_mc.mySlider_mc.stopDrag();   
    89. }   
    90. }  

     

    Yah I know that was a lot of code, and it might look a bit confusing, but I promis you, if you try to put it into your own flash file, maybe event delete the comments I made, you will see kind of a paddern, how and why stuff works and how you made a simple mp3 player with actionscript 3.0 :-)

    Best of luck to you all.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (31)
      Tips and Techniques (47)
      Dynamic Content (31)
      Tricks (8)
      Games (97)
      Utilities (21)
      Getting Started (91)
      Video (24)
      Interactivity (43)
      Web Design (29)

    New

    Hot