The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Fade out audio clip when clicking a button?

  1. #1

    Fade out audio clip when clicking a button?

    I have intro audio music playing on a project I am working on. The audio is in the timeline (but doesn't necessarily have to be), and when I click a button to move away from it, it just keeps playing. I can use SoundMixer.stopAll(); to make is stop as you click the button, but this makes a rather harsh abrupt stop to the music that is undesirable. I would like to have it fade the audio track out once the button is clicked.

    Does anybody know how would I go about doing this?

  2. #2
    TweenMax has a volume property.
    ie: you can tween the volume of a sound.
    http://www.greensock.com/as/docs/twe...umePlugin.html
    I used to be a paranoid-psychotic-schizophrenic, but don't worry, he's dead now.

  3. #3
    Quote Originally Posted by snickelfritz View Post
    TweenMax has a volume property.
    Wow, I didn't know it. Cool! Does TweenLite have it too?

  4. #4
    I have heard of TweenMax before, and tried to get it to work but couldn't. Do you know what I need to do to get it installed and functioning? I have downloaded it, and read the "Usage", but it didn't seem very complete. Is there anywhere else to go for more info on how to use this?

  5. #5
    Okay. I got TweenLite working. It seems to be loading the sound, but now I want the sound to fade out after a button click. Anyway to do that?
    I couldn't find that functionality on their documentation.

  6. #6
    Does not the link posted by snickelfritz work? It seems to be well documented. Take a look at http://www.greensock.com/tweenlite/, at the Plugin Explorer section. There is a usage example. I haven't tried it but it seems that the volume plugin is included in the free package you have downloaded. Hope it helps.

  7. #7
    Quote Originally Posted by giobongio View Post
    Does not the link posted by snickelfritz work? It seems to be well documented. Take a look at http://www.greensock.com/tweenlite/, at the Plugin Explorer section. There is a usage example. I haven't tried it but it seems that the volume plugin is included in the free package you have downloaded. Hope it helps.
    Yeah, i looked into and I found this code:

    Code:
    import com.greensock.*;
    
    
    var someSound = new Sound(new URLRequest("audio_new/flatwound_-_The_Long_Goodbye.mp3"));
    var someChannel = new SoundChannel();
    var someTransform = new SoundTransform(1);//volume is set to the max
     
    someChannel = someSound.play(0, 99, someTransform);//The sound will start from 0 millisecond, will loop 99999 times and will use the someTransform SoundTransform
     
    //the next lines are for fading out the volume
     
    TweenLite.to(someSound, 1, {volume:0, onUpdate:updateChannel, onComplete:stopSound});
     
    function updateChannel():void{
      someChannel.soundTransform = someTransform;
    }
     
    function stopSound():void{
      someChannel.stop();
    }
    ANd it loads and plays the audio perfectly, but I need to add a function to a button to fade the volume down and turn it off. It seems like the last two functions do that, but I don't understand them enough to know how to make a button do those things, and I can't find anything anywhere that tells me how to do that, even though they have an example of it, but I can't find the code for the example.

    Any hints?

  8. #8
    How do I use the TweenLit.to?

  9. #9
    I have never used the TweenLite's onUpdate function, but if I had to perform an audio fadeout from a button I would do something like this:
    Code:
    import com.greensock.*;
    import flash.events.MouseEvent;
    
    var someSound = new Sound(new URLRequest("audio_new/flatwound_-_The_Long_Goodbye.mp3"));
    var someChannel = new SoundChannel();
    var someTransform = new SoundTransform(1);//volume is set to the max
     
    someChannel = someSound.play(0, 99, someTransform);//The sound will start from 0 millisecond, will loop 99999 times and will use the someTransform SoundTransform
     
    //the next lines are for fading out the volume
    // fadeOut_btn is a button you've created on the stage
    fadeOut_btn.addEventListener(MouseEvent.CLICK, soundFadeOut);
    
    
    function soundFadeOut(evt:MouseEvent):void
    {
      TweenLite.to(someSound, 1, {volume:0, onComplete:stopSound});
    }
     
    function stopSound():void
    {
      someChannel.stop();
    }
    Let me know if it works.
    Last edited by giobongio; April 11th, 2012 at 02:49 AM.

  10. #10
    Quote Originally Posted by giobongio View Post
    I have never used the TweenLite's onUpdate function, but if I had to perform an audio fadeout from a button I would do something like this:
    Code:
    import com.greensock.*;
    import flash.events.MouseEvent;
    
    var someSound = new Sound(new URLRequest("audio_new/flatwound_-_The_Long_Goodbye.mp3"));
    var someChannel = new SoundChannel();
    var someTransform = new SoundTransform(1);//volume is set to the max
     
    someChannel = someSound.play(0, 99, someTransform);//The sound will start from 0 millisecond, will loop 99999 times and will use the someTransform SoundTransform
     
    //the next lines are for fading out the volume
    // fadeOut_btn is a button you've created on the stage
    fadeOut_btn.addEventListener(MouseEvent.CLICK, soundFadeOut);
    
    
    function soundFadeOut(evt:MouseEvent):void
    {
      TweenLite.to(someSound, 1, {volume:0, onComplete:stopSound});
    }
     
    function stopSound():void
    {
      someChannel.stop();
    }
    Let me know if it works.

    No, that doesn't work. If I put the even listener on the first frame, it does't do anything. And If I put the button script on the frame where I want it to trigger, then it throws a "unknown function" error.

    Any other ideas?

  11. #11
    Here's an example that uses a movieclip as button that starts or stops the sound.
    the movieclip has 2 keyframes: "play", which shows a standard play icon, and "pause" which shows a standard pause icon.

    The movieclip is on the stage and named "btn".
    clicking btn toggles play and pause, and tweens the volume up or down accordingly.
    The code is fairly self-explanatory.

    BTW, use TweenMax for this; the volume plugin is activated by default.
    PHP Code:
    import com.greensock.*;

    // create the sound and sound channel; play the sound
    var sound = new Sound(new URLRequest("audio/music.mp3"));
    var 
    channel = new SoundChannel();
    channel sound.play();

    // track the state of pause/play
    var muted:Boolean false;

    // remember the channel positon when the music is stopped
    var channelPos:Number 0;

    // init the button
    btn.gotoAndStop("pause");
    btn.addEventListener(MouseEvent.CLICKonClick);

    // check the state of muted; 
    // start or stop sound; 
    // tween volume; 
    // toggle button graphic; 
    // toggle muted
    function onClick(e:MouseEvent):void
    {
        if(
    muted)
        {
            
    // play sound; fade up
            
    btn.gotoAndStop("pause");
            
    channel sound.play(channelPos);
            
    TweenMax.from(channel1, {volume:0});
        }
        else
        {
            
    // fade out; stop sound
            
    channelPos channel.position;
            
    btn.gotoAndStop("play");
            
    TweenMax.to(channel1, {volume:0onComplete:channel.stop});
        }
        
        
    muted = !muted;

    I used to be a paranoid-psychotic-schizophrenic, but don't worry, he's dead now.

  12. #12
    The thing I am trying to say here, is I need the sound click to load and start playing on Frame 1. But way down when you hit frame 45, that is where I want the fade to start. Trigger the fade to begin on frame 45, not when the thing loads immediately. If I move the script off the frame that loads the audio, then it errors out.

    What I need:

    1) The audio track to load when you load the site.
    2) The audio track to fade out when you hit specified frame. (or it could be triggered by a button on specified frame)


    I wouldn't think this would be that hard. Anyone have any idea how to do this?

  13. #13
    It's a file construction issue. Not a code issue.
    Keyframe 45 is not connected to the code and objects on keyframe 1.
    Maintain that connection, or don't move the playhead.
    I used to be a paranoid-psychotic-schizophrenic, but don't worry, he's dead now.

  14. #14
    That's what I am asking. How do you maintain that connection? The playhead has to be moved.

  15. #15
    Not sure. I never move the Main playhead in my projects, and I imagine it would depend on how your file works with the playhead. ie: built-in dependencies, etc...
    I used to be a paranoid-psychotic-schizophrenic, but don't worry, he's dead now.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012