A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: An AS3 Mute Button Example

Threaded View

  1. #1
    Senior Member etuom's Avatar
    Join Date
    Sep 2006
    Location
    Minnesota
    Posts
    193

    Thumbs up 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

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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center