|
||||||
|
||||||
Flash - Volume Controls

Free Flash Tutorial
![]()
The aim of the tutorial is to learn how to use ActionScript to play a sound file, stop a sound file and create a slider that is used to control the volume.
You can follow the same steps for both a horizontal or vertical volume slider. There is only one difference: The document size. Other than that everything is exactly the same. Once finished you simply rotate the vertical slider to convert it to a horizontal slider. None of the ActionScript or other details need to be changed.
Example: Download the Flash & MP3 files Int 115a & Int 115b
Click the play triangle to start.
Once created the slider can be rotated and made into a horizontal volume control.
Example: Download
the Flash & MP3 files Int 115c & Int 115d
Example of Flash Movie with Play, Stop and Volume controls.
Credits: The MP3 files used in these Flash Movie have been submitted by Buffalo Audio: A site offering professional voice-over narration and custom music/sound effects
Step One: Setting Up the Document
Step Two: Creating the Slider
First we will create the horizontal volume control bar: ![]()

Step Three: The Main Stage - Slider
The Main stage should still be empty. We need two buttons and the slider on stage.
For Horizontal Sliders only:
Step Four: The Main Stage - Stop Button
Note: If you do not wish to make your own buttons use some buttons from the Common Library. You will still need to give them an Instance Name (step 4.9 and step 5.14): Window > Common Library > Buttons
If you take a button from the Common Button Library avoid Knobs, Faders and Component buttons as they work differently.
Step Five: The Main Stage - Play Button
The easiest way to draw a triangle is to create a square and convert it to a triangle! If anyone knows an easier way let me know.




Step Six: The Sound file
You now need a short sound file to loop. In can be an: MP3, WAV or AIFF file.
If you wish you can download the MP3s used in the Flash movies on this page:
Download
the MP3 files Int 11b & Int 11d
Step Seven: The ActionScript
Place the following ActionScript in frame 1:
myMusic = new
Sound(this);
myMusic.attachSound("mySound");
myMusic.start(0, 99);
slider.control._y = -50;
slider.control.onEnterFrame = function() {
myMusic.setVolume(0-this._y);
}
slider.control.onPress = function() {
startDrag(this, false, this._x, -100, this._x, 0);
}
slider.control.onRelease = function()
{
stopDrag();
}
stopButton.onRelease = function() {
myMusic.stop();
}
playButton.onRelease = function() {
myMusic.start(0, 99);
}
You will want to test your movie as it is now all done.
ActionScript Explained
myMusic = new Sound(this);
Makes a new sound object in the timeline. This sound object does not yet contain
any sound yet. It is like a CD player with out a CD.
myMusic.attachSound("mySound");
This attaches the sound from the Library which we called "mySound".
myMusic.start(0, 99);
This is an auto-start. It instructs the sound file to start to play. (The files
at the top of this page do not have this - on my files you must hit the play
button). Once the file starts to play it will loop 99 times.
slider.control._y =
-50;
Sets the initial volume to 50% full volume. 0 is
off and -100 is full volume. Remember that
-100
is the top of the slider scale (step 2.20 above).
slider.control.onEnterFrame = function()
{
myMusic.setVolume(0-this._y);
}
Sets the volume to be what every the Y value of slider control. As we slide the
controller up and down the volume goes according to the Y value.
The(0-this._y)converts all the negative numbers
to positive so the volume scale is actually 0 to 100 (not 0 to -100).
Note - Rotation: What is strange is that if you rotate the slider on the main stage to make a horizontal control - the symbols nested inside are not considered to have been rotated. This means that the Y value still works! You do not need to convert the ActionScript to an X value.
Note - Size: This same is also true of size. If you change the size of the controller on the main Stage the Y scale inside the re-sized controller is still 0 to -100. The sizing on Stage makes no difference to measurements of objects nested inside a symbol.
slider.control.onPress = function()
{
startDrag(this, false, this._x,
-100, this._x,
0);
}
This makes the slider move when it is dragged with the mouse. Its movement is
restricted between 0 and -100.
slider.control.onRelease = function()
{
stopDrag();
}
When you let go of the mouse the slider will stop moving.
stopButton.onRelease = function()
{
myMusic.stop();
}
When you click on the stop button it will stop the sound from playing.
playButton.onRelease = function()
{
myMusic.start(0,
99);
}
When you click on the play button the sound file will play and loop 99 times.
Well except for some foot notes that's it - I hope you found this tutorial useful.
Note: This method uses in this tutorial is: Event sound
Cross Ref: For a beginners tutorial on the difference between Event and Streamed sound see: Sound
Cross Ref: For a practical tutorial on how to compress Flash movies with embedded sound files see: Optimizing a Flash Movie
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|