Flash CS3 Tutorial – Volume Toggle Switch
Flash Tutorial April 22nd, 2008Today's tutorial is Part 4 of a series of tutorials on creating a music player using Flash CS3 and ActionScript 3. The previous tutorials can be found here:
If you haven't read these tutorials yet, then I strongly urge you to check them out. The Flash file we'll be starting with can be found at the bottom of the Pause Button tutorial.
1. Open up pause.fla and take a look at the ActionScript. This file contains all the ActionScript we've worked on to date. If there are parts of this ActionScript that you don't understand, again I'll point you to the previous music player tutorials listed above.
As a quick review, we've created a Sound object (an instance of the Sound class), and we've stored this object in a variable called music. After creating the Sound object, we've also created a SoundChannel object and stored it in a variable called sc. This SoundChannel object allows us to control the playback of our Sound object. In order to control the volume, we must also create a SoundTransform object.
2. Create a Mute button. We've already created a volume slider, but we also want to be able to simply click on a Mute button in order to immediately silence the sound. Here's what my music player looks like with the Mute button added.

If you don't want to draw the shape for the mute button yourself, you can access it within the Webdings font, if you have it. To access the speaker icon, select the Webdings font and type a capital X.
3. Set up the Mute button Movie Clip. Save the Mute button graphic as a Movie Clip symbol. This Movie Clip is going to have 2 states: an On state for when the sound is playing, and a Mute state for when it's not. Within the Mute movie clip timeline, create 2 different states, and in the "Mute" state, draw a red line across your mute icon. Here's what my timeline looks like:

Be sure to add a "stop" action to frame 1 of your movie clip's timeline so Flash doesn't play through the entire timeline by default. Also, don't forget your frame labels, or the code we enter in below won't work.
4. Go back to your main timeline and give your mute movie clip an instance name of mute_mc.
5. Create a variable to store your current volume level and your muted status. In the Scene 1 Timeline, click on frame 1 for the Actions layer and hit Opt+F9 on your Mac (or just F9 on PC) to open up your Actions Panel. At the very top of your code, create 2 new variables called 'soundVolume' and 'muted'. Set soundVolume equal to 1 and set muted to false. Like so:
var soundVolume:Number = 1;
var muted:Boolean = false;
We've set the default value of soundVolume to 1 because we want to start out at full volume by default.
6. Update the 'adjustVolume' function. In the adjustVolume function, we had previously created a variable called vol and used this variable to set our volume. Since we now have a more global volume variable, we can now point to soundVolume instead of creating a whole new variable. Here's what the updated function will look like:

Notice that in this function, we've also set the muted variable to false. This will ensure that if the user clicks on the mute button and then clicks on the volume slider, Flash will set the muted status back to where it needs to be.
6. Update the dropIt function. In our volume slider tutorial, we overlooked one little thing. We created an ENTER_FRAME event handler for when we clicked and dragged the volume slider. This ENTER_FRAME event constantly checks the position of the volume slider and updates the volume accordingly. Unfortunately, though, we never deleted this ENTER_FRAME event when we released the mouse button to stop dragging. If we don't do this now, then the ENTER_FRAME event will mess with our mute button. Here's what the updated dropIt function will look like:

7. Enter the code for your mute button. Enter the following code at the bottom of your existing code:

Line 81 creates an event listener that will trigger the mute function when the mute button is pressed.
Inside the mute function, we've put everything inside an 'if' statement that checks to make sure that sc is not null before it runs the code. This ensures that we're not trying to perform volume adjustments to a sound that hasn't even been loaded.
It's important to understand at this point that our soundVolume variable was created in order to keep track of the UNMUTED volume of the sound. When the sound is muted, this variable is never set to zero. Rather, this variable retains the previous volume so that when we UNMUTE the sound, it will return to the volume that the user had previously set.
Inside the initial 'if' statement, there is another 'if' statement that checks the value of our muted variable. Since muted is a Boolean value, we don't have to type out "if (muted == true)". Instead, we can simply say "if (muted)", which amounts to the same thing.
If the sound IS currently muted, then we want to UNmute it. So, we set the volume back to its previous volume in lines 90-91 and then set the muted variable back to false. Also, on line 92, we send the mute_mc movie clip's timeline back to the "On" state.
If the sound is NOT currently muted, then we do the opposite.
8. Test the movie. If we test the movie now, we'll notice a couple problems when we start playing around with the controls. First of all, the mute button doesn't act like a button. The mouse cursor doesn't turn into a hand cursor. In fact, you'll notice that it's the same for the volume slider. This is an easy fix. Add the following lines of code to the very top of your ActionScript:
mute_mc.buttonMode = true;
volume_mc.slider_mc.buttonMode = true;
Another thing you may notice is that if you hit the mute button and then drag the volume slider around, the volume returns, but the mute button graphic still shows that it's muted. This is an easy fix as well. Simply add the following line of code anywhere within the adjustVolume function:
mute_mc.gotoAndPlay("On");
And that's it! Our file is complete. Here's what it looks like:
Here are the source files: mute.zip


April 23rd, 2008 at 3:21 pm
[...] Flash CS3 Tutorial – Volume Toggle Switch [...]
April 23rd, 2008 at 3:34 pm
[...] Flash CS3 Tutorial – Volume Toggle Switch [...]
May 1st, 2008 at 7:43 am
I really apreciate you tutorials, and i wonder if you are going to publis a tutorial for “next” and “previous” track-buttons here? That would be awsome! thanks
May 4th, 2008 at 8:23 am
Hey Craig
i’ve noticed a thing with the music player, if you lower the sound volume and pause the music and then hit the play button the sound volume goes up 100% again and the volume slider is where i left it. How can I make start with the same volume i paused it in?
again really good tutorials, keep it up!
(i come from sweden so my english isn’t very good but i hope you can understand me anyway)
July 9th, 2008 at 5:31 am
Hi Jensen,
call adjustVolume(e) function right after sc=music.play(pos); in playMusic function.
define your playMusic function as below:
function playMusic(e:Event):void
{
sc=music.play(pos);
adjustVolume(e); /* will adjust Volume */
isPlaying=true;
}
this will solve your problem…
July 9th, 2008 at 6:03 am
the link below contains what i’ve done so far with the help of Craig’s tutorial…
http://rapidshare.com/files/128357612/Flash_Audio_Player.rar.html
besides what have been told Craig’s tut, i added a little extra codes for more functionality.
you may check it…
August 14th, 2008 at 9:30 am
Craig,
Can you tell me how I can have the movie start without pressing play?
August 29th, 2008 at 12:17 am
very thankfull for this tutorial
i got problem (Bugs) in this player
i have completed the music mp3 player
click on play button
click on mute
then click on pause
then click on play again
you will see the music will play but mute button is still activate
September 12th, 2008 at 7:21 am
I added a voice over using this to an existing flash movie that I created that is a basic image rotation. Every time the movie loops the voice over starts again over the top of the original. How can I keep the music from starting over every time it loops? Also, the voice over is longer than the image rotation so how can i keep it playing when the images loop? I hope this all make sense. Thank you!
November 23rd, 2008 at 10:32 am
Great tutorial! It’s really helped me with my coursework!
one question though:
Im struggling to get another one of these on the same scene. i could really do with the help i have been at it all day!!!!
thanks in advance
Rob
December 3rd, 2008 at 11:08 pm
Thanks for the great tutorial, but I’m wondering how I can create a Toggle volume on/off button that references a Voice Over that’s streaming within the Flash file. I can’t have an external sound file that may or may not sync up to the animation of the file. How would I code that? Any help would be greatly appreciated. Thank you!
Darcie
December 31st, 2008 at 9:45 pm
Walk right in.. sit right down.. Daddy, let your mind roll on…
Great Tutorial! Very thorough and clear explanations.
Mehmet suggested using adustVolume(e) to fix the pause/play/volume issue, here is an alternate version.
Within playMusic()..
Add the following two lines after “sc = music.Play();”
var st:SoundTransform = new SoundTransform(soundVolume);
sc.soundTransform = st;
This is essentially the same thing Mehmet did, but it may help you see why it works.
March 27th, 2009 at 5:56 pm
very good
April 30th, 2009 at 1:09 am
really awesome tut. At the end I was wondering about a couple of those things, and then I saw you wrote about them.
May 22nd, 2009 at 11:44 pm
Again Craig,
like person wrote on top Can you tell me how I can have the sound start without pressing play?
August 6th, 2009 at 4:59 pm
Really great tutorial…everything is explained so well that I found it easy to edit for my own needs.
Thanks
August 14th, 2009 at 12:08 pm
i would like to make a correction about :
mute_mc.gotoAndPlay(“On”);
it should be
mute_mc.gotoAndStop(“On”);
that should fix the mute button problem when you slides the slider
November 6th, 2009 at 9:15 pm
Thanks! The tutorial is very clear. All you need for a nice handy player is here.
As i’m using more than one stage in my flash file, i was wondering if there were a way to keep the sound volume and the volume slider from going back to initial level when shifting from one stage to the other??? (as for play and stop buttons, they work well even in multiple stages)
Thanks to anyone who could give me a hint…
December 20th, 2009 at 12:12 pm
Hi.
I don’t understand this…
I`ve downloaded source and then i’ve edited to load a shoutcast streamming ip. App almost perfectly functions, but, sometimes, when i open it, its buttons show themsefves inactive.
I am working to correct it but i`m stopped…I Don’t Understand.
Can u help me?.
Thanks!
December 22nd, 2009 at 8:48 pm
Mehmet
Based on your app, i’ve created mine own and i’ve used a shoutcast adress instead a local sound…how do u do to automatically load sound, so that, no need to press START to start ear?
Thanks!
December 24th, 2009 at 1:11 am
1.- I need the sound starts without press play button.
2.- After i stop, i need to re-start (Play button) justly where i`ve stopped, instead at begin of sound.
How to?
December 25th, 2009 at 7:31 pm
Hi.
How to kill sound when i press Stop Button?.
I`m ussing this to broadcast radio online.
Each time i press Stop Button , then Play Button, it reload Sound at start..I need when i press Play Button again, sound is refreshed!.
i hope you understand me!
Cheers!.
December 28th, 2009 at 6:35 pm
Solved!
To Auto-Load Sound.
I’ve added this near of Line 30
//AutoPlay
sc=music.play(pos);
//isPlaying true
dyn_txt.text=”Playing…”;
To Refresh Streammed Sound:
I`ve added this since Line 67 (Stop Routine) to kill old sound and Re-Load the same sound in real time:
function stopMusic(evt:MouseEvent)
{
if(sc!=null)
{
sc.stop();
sc = null;
music = new Sound(new URLRequest(“http://estelarisradio.net:8078/;”));
pos=0;
isPlaying = false;
mute_btn.gotoAndStop(1);
volume_mc.slider_mc.x=100;
dyn_txt.text=”Stopped…”;
}
}
Notice this:
music = new Sound(new URLRequest(“http://estelarisradio.net:8078/;”));
This solve the issue on Stop and Play again…Sound is refreshed!
Too Thanks!
January 15th, 2010 at 4:24 pm
Hi again!.
How to add a balance control to my player?.
Too Thanks!
January 20th, 2010 at 10:35 pm
Solved again!
You can see the final app here:
http://www.estelarisradio.net/dos
Great Tuto!
Cheers and Thanks a lot!
Gabriel.
January 21st, 2010 at 3:52 pm
Hi!
To add a progressive balance change to right i’m ussing this code (functions!).
//Start Fade Center Right by ussing Mat.abs function//
var req:URLRequest = new URLRequest(“1.mp3″);
snd.load(req);
var panCounter:Number = 0;
var trans:SoundTransform;
trans = new SoundTransform(1, 0);
var channel:SoundChannel = snd.play(0, 1, trans);
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
trans.pan = Math.abs(panCounter);
channel.soundTransform = trans; // or SoundMixer.soundTransform = trans;
panCounter += 0.05;
if(panCounter == int(panCounter))
{
panCounter == 1
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
function onPlaybackComplete(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
//End Fade Center Right by ussing Mat.abs function//
How to do to obtain “zero” (0) and stop calculation routine?
This is for stablish “zero” as Volume value!.
Thanks again!.
January 24th, 2010 at 9:30 pm
Thank you very much for this great tutorial.
I do have one favor to ask, though. Could you please tell us how to make the music loop endlessly?
I’ve tried figuring this out on my own, but have had no luck. I urgently need it.
Thank you.