Skip to Content

Flash Sound - How to create a volume slider

Author: Bill Trikojus

This tutorial will show you how to make a basic sound controller using Flash Actionscript.  The controller will allow the user to start and stop the sound and also adjust the volume using a slider.  If you have background music on your website it is essential that you allow the user to control it as many users find it annoying.

1

Document setup

2

Create a slider

3

Create a slidebar

4

Add buttons

5

Import sound

6

Actions

7

Actions explained

 
 

1

Document setup

 


OK I thought I would show you how to control a sound with actionscript.

Here's what you will me making.



OK start by loading Flash MX. Double click on the fps to open the Document Properties window - change the settings to these -

Document Properties

Click OK.

 

2

Create a slider

 


Now make 2 new layers so that you have three in total. Name them "actions", "slider" and "buttons".

Create 2 new layers

Select frame 1 of the slider layer and draw a 5px X 70px rectangle - I chose to have no stroke and a fill colour of #999999. Select your rectangle and align it so it's centered horizontally and just a little bit up from the bottom of the stage. Hit F8 to convert it to a movieclip. Name it "sliderMC" and make the registration point is set to the bottom-center.

Convert to symbol

Click OK. Your new MC should be selected on the stage

select MC

Give it an instance name of "slider". Save your file as soundslider.fla into a folder called whatever you want.

 
3

Create a slidebar

 
 


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.

convert to symbol

Click OK and give it an instance name of "slideBar".

 
4

Add buttons

 
 


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.

 
5

Import sound

 
 


OK now get yourself a short audio loop (can be an mp3, wav or aiff). 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 up like so -

Linkage Properties

 
6

Actions

 
 


OK let's get into the scripting. Select frame 1 of the actions layer and add these actions -

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();

 
7

Actions explained

 
 


Let's go through that bit by bit.

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.

You can download the source file for this tute here.

bulletBack


Share

Like this? Click a link below to share it...


Subscribe and Download

Subscribe to the Swinburne Faculty of Design Podcasts

Comments

2009-07-22: AJP said:
Cool! 10/10
I liked this tut very explanitory, and it works great! Unlike the other one I had... (playd twice when double click play btn and no volume)
Bravo! Thanks again.

2009-12-13: Peter Wallhead said:
Hey, this tutorial is great!

I couldn't find out how to stop the music from starting/looping over every time the play button was pressed, but disabling the play button is the simplest solution.

2010-10-31: Samit said:
It's really cool, clear and gr8! thanks

2011-05-30: abe said:
That's a great lesson for Me..
Thanks it really help me with my job.

2011-09-01: ajay said:
really, great tutorial man....such a easy way in which u described. keep posting

2012-03-06: Nodnarb3 said:
How can you make the volume slider horizontal? Ive tired a few things but cant get it to work... Please help!

2012-03-06: Nodnarb3 said:
ok now having problem with releasing the mouse. Once mouse is released (on slider) when you want to adjust volume again, the slider doesnt move past the position you released it in last. Please Someone help!

Post a comment

Garbage posts and SPAM will be deleted.

* Name:
* Email: (will not be made public)
* Comment:
* Reply Notification: