|
-
Senior Member
An AS3 Mute Button Example
Playing around with the Sound class, SoundChannel(), and SoundTransform(), and conditinal statements, I came up with a mute button that works in Koolmoves. Seems this one gets asked for alot and there are many ways to play and handle sound. This one works.
Any suggestions, improvements and discussion are welcome. Maybe this should be a class or something. Anyway go ahead and try it out and let me know what you think.
Actionscript Code:
/*This example uses embeded sound but the function part should work for external sound too. We add a sound file (mp3) to the Symbol Library with class name "Ambient", and add a sound button from library to the stage and rename it "muteBtn" */
//this part can be modfied to the users preferences.
var ambientSound:Ambient = new Ambient(); var scChannel:SoundChannel = new SoundChannel(); var stTransform:SoundTransform = new SoundTransform();
/*here is the function for the Mute Button ********************************************** */
muteBtn.addEventListener (MouseEvent.CLICK,setVolumeHandler); function setVolumeHandler(evt:MouseEvent):void{ if (stTransform.volume > 0){ stTransform.volume = 0; }else{ stTransform.volume = 1; } scChannel.soundTransform = stTransform; }
//****************************************************
scChannel = ambientSound.play(0,4); //play(start position,loops) just because I have a short clip
Hope this is helpful!
edit: I can see that "else" statment that returns the volume to .5 (50%) could be better if it returned it to whatever volume the sound was when it was playing. Probably have to define a variable that would hold whatever volume the sound is at.
UPDATE
edit #2 changed "else" statements volume to "1" (100%) Thats what it needed. I suppose if we had volume controls we could pass that value back to the function but then we might as well have a mediaplayer. If we had multiple sounds I think we would want a stopAllSounds().
Last edited by etuom; 07-20-2010 at 02:17 AM.
Reason: cleaned up code presentation, changed some paraameters
-
Much appreciated. It is asked for a lot. I suggest submitting it to KoolExchange.com.
It can also be added to the AS3 script library. The purpose of the library was to share useful code. After the code is in final form, email me the code and I will add it. It is possible to parameterize the code with xml (see readme.txt in Bin/AS3/Script Library).
Last edited by Bob Hartzell; 07-20-2010 at 06:19 AM.
-
Senior Member
Submitted a mute button fun file to the exchange and found it did not work...re-submitted with the sound file. muteButton.zip. Can the original be deleted? (UPDATE: Bret took care of it)
I also emailed the xml to you, Bob.
Also, my copy of Koolmoves does not have a readme file in that location. I just work off the stuff that is supplied.
Last edited by etuom; 07-23-2010 at 03:40 PM.
-
I tried your example - but I wanted to mute an already exisiting mediaplayer.
The reason being I want my mute parton on a different part of screen than the media players one.
what is wrong my code here? mediaplayer is named 'mediaplayer1'
----------------
var scChannel:SoundChannel = new SoundChannel();
var stTransform:SoundTransform = new SoundTransform();
muteBtn.addEventListener
(MouseEvent.CLICK,muteButtonHandler);
function muteButtonHandler(evt:MouseEvent):void{
if (stTransform.volume > 0){
stTransform.volume = 0
}else{
stTransform.volume = 1;
}
scChannel.soundTransform = stTransform;
}
scChannel = mediaplayer1.play();
---------------
any tips?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|