5 Replies Latest reply on Oct 27, 2010 1:59 PM by kglad

    Sound Fade - a bit over a period of time - AS 2

    Steve_Nixon_ADP Level 1

      Hi there, this is probably a copy and paste from an older post but...

       

      Does anyone have a script in actionscript 2 that fades the sound level of a specific audio file over a specified period of time to a specified level?

       

      Eg:  set the level of mymusic.mp3 to 2  (on a scale of 10) over 4 seconds

       

      Best, Steve

        • 1. Re: Sound Fade - a bit over a period of time - AS 2
          kglad Adobe Community Professional & MVP

          call fadeSoundF() and pass the movieclip where the sound is instantiated, the sound, the end volume and the number of seconds over which you want to fade.  nothing below should be changed.

           

           

          function fadeSoundF(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void{

          clearInterval(mc.fadeI);

          var volumeInc:Number = vol-s.getVolume()/(10*sec);

          mc.fadeI=setInterval(fadeF,100,mc,s,volumeInc,vol);

          }

          function fadeF(mc:MovieClip,s:Sound,inc:Number,endVol:Number):Void{

          s.setVolume(s.getVolume()+inc);

          if(Math.abs(s.getVolume-endVol)<inc){

          clearInterval(mc.fadeI);

          }

          }

          • 2. Re: Sound Fade - a bit over a period of time - AS 2
            Steve_Nixon_ADP Level 1

            Hi Kglad, thanks for the reply - greatly appreciated. So to be clear, the sound should be playing in a movie clip and the reason there is no specific .mp3 file targeted is becasue it addressing whatever is playing in that specific clip?

             

            So this line:     (mc:MovieClip,s:Sound,vol:Number,sec:Number):

             

            Should have my specifics in it such as  (mc:MyClip2,s:Sound,vol:2,sec:5):

             

            Is this how I detail the specific values I want to set?

             

            Best, Steve

            • 3. Re: Sound Fade - a bit over a period of time - AS 2
              kglad Adobe Community Professional & MVP

              all sounds are associated with a movieclip.  each different sound must be associated with a unique movieclip if you want to control the sounds indepentedly.  so, you need to reference that (or some other movieclip unique to each sound) IF you ever want to simultaneously fade two different sounds.

               

              if that situation never occurs (ie, you only have one sound), you can delete all movieclip references in the code i suggested and everything will work.

              • 4. Re: Sound Fade - a bit over a period of time - AS 2
                Steve_Nixon_ADP Level 1

                Thanks KGlad, understood. I'll give it a go. Thanks again for your time here.

                 

                Best, Steve

                • 5. Re: Sound Fade - a bit over a period of time - AS 2
                  kglad Adobe Community Professional & MVP

                  here's the code if you only have one sound at any one time that you'll fade:

                   

                   

                  var fadeI:Number;

                   

                  function fadeSoundF(s:Sound,vol:Number,sec:Number):Void{

                  clearInterval(fadeI);

                  var volumeInc:Number = vol-s.getVolume()/(10*sec);

                  fadeI=setInterval(fadeF,100,mc,s,volumeInc,vol);

                  }

                  function fadeF(s:Sound,inc:Number,endVol:Number):Void{

                  s.setVolume(s.getVolume()+inc);

                  if(Math.abs(s.getVolume-endVol)<inc){

                  clearInterval(fadeI);

                  }

                  }