Thursday, 8 October 2009

voloume controller in action script

1 create new flash document 50px w by 100px h and frame rate = 21fps.

2 create your slider,Give it an instance name of "slider". Save your file as soundslider.fla into a folder called whatever you want. Now double click on the new mc on the stage to go inside it. Make a new layer and draw a new rectangle - 25px X 5px. Select your new rectangle and look at the align panel. Make sure that to stage is selected and center it vertically and horizontally. Now convert it to a movieclip - name it slideBarMC and set the registration point to the center.

3 OK go back to the main timeline. We'll set up the last couple of graphics before we get into the scripting. Select frame 1 of the buttons layer and (up the top of the stage) draw a square and triangle to use as the stop and play buttons. Select the square, convert it to a button named "stopBut" and give it an instance name of stopB. Select the triangle, convert it to a button named "playBut" and give it an instance name of playB.

now get yourself a short audio loop (can be an mp3, wav or wav). In flash, select File/Import to library and locate your sound. Import your sound then click on it once in the library to select it. Open the drop down menu up the top right of the library and choose "linkage". Set the linkage.

then add a new layer called actions.
write the following.

bgSound = new Sound(this);bgSound.attachSound("sound1");bgSound.start(0, 99);playB.enabled=false;slider.slideBar._y = -50;slider.slideBar.onEnterFrame = function() { bgSound.setVolume(0-this._y);};slider.slideBar.onPress = function() { startDrag(this, false, this._x, -65, this._x, 0);};slider.slideBar.onRelease = slider.slideBar.onReleaseOutside=function () { stopDrag();};stopB.onRelease = function() { bgSound.stop(); playB.enabled=true; stopB.enabled=false;};playB.onRelease = function() { bgSound.start(0, 99); playB.enabled=false; stopB.enabled=true;};stop();

//dont include the folling in your code

Line 1 - we make a new sound object on "this" timeline. The "this" is very important because if you do not sprecify a target here then flash will assume we want to make the new object on level0 which would cause us problems later on if we wanted to load our sound slider into another movie.

Line 2 - We attach the sound in our library to our new sound object. Remember that "sound1" was the linkage name that we gave our sound.

Line 3 - We start out sound object. The "0" represents the number of milliseconds from the start of the sound that we want flash to start playing - 99.9% of the time you will set this 0. The "99" represents the number of times we want flash to loop our sound.

Line 4 - Turn off the play button (we dont need it at the moment because the sound is already playing).

Line 5 - Set the starting y position of our slideBar

Lines 6-8 - This tells Flash to set the volume of our sound object to whatever the y position of our slideBar is. Now because we are getting the y value of the slideBar from within the sliderMC, we need to convert the negative y value into a positive number to set the volume to. We do this with "0-this._y". So if the slideBar was at a y position of minus 43 then we would set the volume of our sound object to 0 - -43 which = 43.

Lines 9-14 - This sets up the dragging action for our slideBar and constrains it to a rectangle.

Lines 15-19 - Our stop button action. First line stops the sound object. Second line turns on our play button. Third line turns off the stop button.

Lines 20-24 - Our play button action. First line plays the sound object. Second line turns off our play button. Third line turns on the stop button.

OK save your fla and then test it.

OK I think that's about it.


No comments: