15 Replies Latest reply on Jan 5, 2012 10:42 AM by kglad

    Function to fade audio smoothly from one level to another in AS2

    PeteGould Level 1

      Greetings all.

       

      I'm a newcomer to Actionscript programming and trying to modify a Flash site template downloaded from TemplateMonster.com (which is an AS2 template).  I've almost succeeded at what I need to do but have run into a couple of brick walls.  One is that I've added a video player to the site and need to make the background music track smoothly fade out when the video starts and fade back in when it ends (or is stopped).  I set up a listener object for the video player that works.  It's the smooth fade of audio levels that doesn't.

       

      I found an excellent thread from last year (http://forums.adobe.com/message/3236495) in which kglad address the issue of fading audio from one level to another.  Since TemplateMonster's templates set up a master movie clip and then load pages to play within it, and I need to call the function from within the pages, I tried setting it up as a global function.

       

      So when the overall site initializes, I have this:

       

      //---------------------------------------------------------------------------------------- -------------

      this.createEmptyMovieClip("mcMusictrackHolder", this.getNextHighestDepth());
      var sndAudio:Sound = new Sound(mcMusictrackHolder);
      var nMaxMusicVolume:Number = new Number(mcMusictrackHolder);

       

       

      nMaxMusicVolume = 30;
      sndAudio.attachSound("MusicTrack");
      sndAudio.setVolume(nMaxMusicVolume);
      sndAudio.start(0,9999);

       

      // Here is kglad's function converted to a global function
      _global.fadeSoundF = function(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void
      {
           trace("Getting here with sound at " + s.getVolume());
           clearInterval(mc.fadeI);
           var volumeInc:Number = vol-s.getVolume()/(10*sec);
           mc.fadeI=setInterval(fadeF,100,mc,s,volumeInc,vol);
           trace("Leaving with sound at " + s.getVolume());
      }

      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);
           }
      }

      //---------------------------------------------------------------------------------------- -------------

       

      Then within the page that contains the video player (a child of the above), I have this:

       

      //---------------------------------------------------------------------------------------- -------------

      var listenerObject:Object = new Object();
      var sCurrentState:String;

       

      listenerObject.stateChange = function(eventObject:Object):Void
      {
      sCurrentState = my_FLVPlybk.state;
      if (sCurrentState == "playing")
      {
        fadeSoundF(_root.mcMusictrackHolder,_root.sndAudio,0,1);

      }
      else
      {
        fadeSoundF(_root.mcMusictrackHolder,_root.sndAudio,_root.nMaxMusicVolume,1);
      };
      };

      my_FLVPlybk.addEventListener("stateChange", listenerObject);

      //---------------------------------------------------------------------------------------- -------------

       

      The listener object is working fine - so if instead of calling fadeSoundF I simply do _root.sndAudio.setvolume(0) to mute and _root.sndAudio.setvolume(30) to restore it works fine except that the volume changes abruptly instead of ramping.

       

      But obviously there's a problem with my effort to convert kglad's function to global and use it that way, because the trace statements tell me the audio is getting set to random levels.  It smoothly ramps all right - but tries to get to -472 or +8212 or other insane numbers.

       

      Any help with where I'm going wrong would be deeply appreciated.  Keep in mind that although I have some long-ago programming experience in other languages, this environment is completely alien to me and you can feel free to assume I'm completely ignorant.  What I've managed to piece together is largely pulled from online research, which is why there's probably an obvious glaring error in there.  Feel free to provide a response that assumes I know nothing.

       

      Best,

       

      Pete

        • 1. Re: Function to fade audio smoothly from one level to another in AS2
          kglad Adobe Community Professional & MVP

          change:

           

          var nMaxMusicVolume:Number = new Number(mcMusictrackHolder);

           

          to

           

          var nMaxMusicVolume:Number = 100; // or any other number between 0 and 100.

          • 2. Re: Function to fade audio smoothly from one level to another in AS2
            PeteGould Level 1

            Thanks - and done.  But sadly it's still doing the same thing.

             

            I just ran it with nMaxMusicVolume set to 30.  When I launched it, the music began and it was at the correct level.  But when the function was triggered from the video player page, the trace output at the beginning of the function said the sound level was 111 (and it tried to ramp it to that level, causing distortion as you'd expect).  When I launched the video and it triggered the function again, instead of setting the level to 0 it set it to 795.  Pausing the video triggered another call to the function which ramped the background audio to -2216.  Starting it playing again brought the background audio level to -1712.  I notice that if I terminate and rerun it, and repeat the same steps, I end up with different numbers.

             

            It's as though an argument isn't getting passed properly and is ending up undefined and picking up pseudorandom content from somewhere.

             

            Pete

            • 3. Re: Function to fade audio smoothly from one level to another in AS2
              PeteGould Level 1

              I added a few more traces.  Here's the new main page:

               

              //---------------------------------------------------------------------------------------- --------------

              this.createEmptyMovieClip("mcMusictrackHolder", this.getNextHighestDepth());

              var sndAudio:Sound = new Sound(mcMusictrackHolder);

              var nMaxMusicVolume:Number = 30;

              //

              sndAudio.attachSound("MusicTrack");

              sndAudio.setVolume(nMaxMusicVolume);

              sndAudio.start(0,99999);

                

              _global.fadeSoundF = function(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void

              {

                        trace("Getting here with level at " + sndAudio.getVolume());

                        trace("asking to set it to " + vol + " in " + sec + " second(s).");

                        clearInterval(mc.fadeI);

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

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

                        trace("Leaving with sound at " + sndAudio.getVolume());

                        trace(" ");

              }

               

              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);

                        }

              }

              //---------------------------------------------------------------------------------------- --------------

               

              Here's sample trace output:

               

              Getting here with level at 30                  <---- this is on first visiting the page where the listener object initializes and asks it to ramp the audio from its

              asking to set it to 30 in 1 second(s).             current level to the same level (probably because the video is buffering or rewinding and that counts as a state change).

              Leaving with sound at 30

               

              Getting here with level at 111                <---- this is a second triggering of the function (probably because the video is STOPPED and that counts as a

              asking to set it to 30 in 1 second(s).            state change after buffering).  As of yet the video hasn't been asked to play.

              Leaving with sound at 111

               

              Getting here with level at 3765               <---- This is what happens when the video is asked to play; since it is asking to leave the level at 30 I'm guessing it is buffering

              asking to set it to 30 in 1 second(s).              again but not playing yet, which is why it's still being asked to set the background to 30 instead of 0.

              Leaving with sound at 3765

               

              Getting here with level at 3765                <---- NOW the video is actually playing, so it asks to set the background audio to 0.

              asking to set it to 0 in 1 second(s).

              Leaving with sound at 3765

               

              Getting here with level at -41740             <---- ... and I hit STOP, which generates a request to bring the background audio back to 30.

              asking to set it to 30 in 1 second(s).

              Leaving with sound at -41740


               

              I should probably replace the if/else on the video player page with a switch/case so the function ONLY gets called when playback actually stops and starts, and doesn't get triggered for every state change.  But I think I still have an error in here someplace...

               

              Many thanks to kglad for your help!

               

              Pete

              • 4. Re: Function to fade audio smoothly from one level to another in AS2
                kglad Adobe Community Professional & MVP

                what's the following show:

                 

                this.createEmptyMovieClip("mcMusictrackHolder", this.getNextHighestDepth());

                var sndAudio:Sound = new Sound(mcMusictrackHolder);

                var nMaxMusicVolume:Number = 30;

                //

                sndAudio.attachSound("MusicTrack");

                sndAudio.setVolume(nMaxMusicVolume);

                sndAudio.start(0,99999);

                trace(this);

                  

                _global.fadeSoundF = function(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void

                {

                          trace("Getting here with level at " + sndAudio.getVolume());

                          trace("asking to set it to " + vol + " in " + sec + " second(s).");

                          clearInterval(mc.fadeI);

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

                if(volumeInc!=0){

                          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)

                          {

                trace("endVol "+s.getVolume());

                                    clearInterval(mc.fadeI);

                          }

                }

                 

                p.s.  as i was typing this i noticed a typo in your code (and possibly mine if you copied it correctly) in the bold italic line

                • 5. Re: Function to fade audio smoothly from one level to another in AS2
                  PeteGould Level 1

                  Hi kglad,

                   

                  Before doing anything else I tried changing just the one line containing the typo (it was a cut-and-paste from the 2010 thread so I guess it was in the original) - but the problem continued anyway.  So I replaced the entire block of code with your modification above to add the additional trace statements.  I also added conditionals in the child page so it ONLY calls the function when the video stops and starts, ignoring extra calls for buffering, etc.

                   

                  Below is what I got for trace output.  I note with interest that it doesn't always reach the trace instruction to print the endvol.

                   

                   

                   

                  _level0        <----- this is when it initializes

                   

                   

                  Getting here with level at 30                  <----- this is when the child page first loads and the listener object triggers as the video loads

                  asking to set it to 30 in 1 second(s).              When this happens the level enters a runaway (note that in the next trace output it has reached 1380)

                                                                                     So just by getting to the page the background music audio level is pegged.

                   

                  Getting here with level at 1380              <----- Hitting "play" on the video

                  asking to set it to 0 in 1 second(s).

                   

                  Getting here with level at -2208             <----- "stop"

                  asking to set it to 30 in 1 second(s).

                  endVol -200

                   

                  Getting here with level at -200               <----- "play"

                  asking to set it to 0 in 1 second(s).

                  endVol 0

                   

                  Getting here with level at 0                    <----- "stop"

                  asking to set it to 30 in 1 second(s).

                  endVol 30

                   

                  Getting here with level at 30                  <----- "play"

                  asking to set it to 0 in 1 second(s).

                   

                  Getting here with level at -129               <----- "stop"

                  asking to set it to 30 in 1 second(s).

                  endVol 0

                   

                  Getting here with level at 0                    <----- "play"

                  asking to set it to 0 in 1 second(s).

                   

                  Getting here with level at 0                    <----- "stop"

                  asking to set it to 30 in 1 second(s).

                  endVol 30

                   

                   

                  Thanks again for all your help with this.

                   

                  Pete

                  • 6. Re: Function to fade audio smoothly from one level to another in AS2
                    kglad Adobe Community Professional & MVP

                    are you loading any swfs?

                     

                    if not, what's the following show:

                     

                    this.createEmptyMovieClip("mcMusictrackHolder", this.getNextHighestDepth());

                    var sndAudio:Sound = new Sound(mcMusictrackHolder);

                    var nMaxMusicVolume:Number = 30;

                    //

                    sndAudio.attachSound("MusicTrack");

                    sndAudio.setVolume(nMaxMusicVolume);

                    sndAudio.start(0,99999);

                     

                    _global.fadeSoundF = function(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void

                    {

                              trace("Getting here with level at " + s.getVolume());

                              trace(mc+" "+s)

                              clearInterval(mc.fadeI);

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

                    if(volumeInc!=0){

                              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);

                              }

                    }

                    • 7. Re: Function to fade audio smoothly from one level to another in AS2
                      PeteGould Level 1

                      Here you go:

                       

                      Getting here with level at 30                                  <---- First accessing the page

                      _level0.mcMusictrackHolder [object Object]

                       

                      Getting here with level at 3000                               <---- "play"

                      _level0.mcMusictrackHolder [object Object]

                       

                      Getting here with level at -14400                            <---- "stop"

                      _level0.mcMusictrackHolder [object Object]

                       

                      Getting here with level at -1170                              <---- "play"

                      _level0.mcMusictrackHolder [object Object]

                       

                      Getting here with level at 0                                     <---- "stop"

                      _level0.mcMusictrackHolder [object Object]

                       

                      Getting here with level at 30                                    <---- "play"

                      _level0.mcMusictrackHolder [object Object]

                       

                      Getting here with level at -18                                   <---- "stop"

                      _level0.mcMusictrackHolder [object Object]

                       

                       

                       

                      (not loading any swf's).

                       

                      Pete

                      • 8. Re: Function to fade audio smoothly from one level to another in AS2
                        kglad Adobe Community Professional & MVP

                        there's another typo i see.  use:

                         

                         

                         

                        this.createEmptyMovieClip("mcMusictrackHolder", this.getNextHighestDepth());

                        var sndAudio:Sound = new Sound(mcMusictrackHolder);

                        var nMaxMusicVolume:Number = 30;

                        //

                        sndAudio.attachSound("MusicTrack");

                        sndAudio.setVolume(nMaxMusicVolume);

                        sndAudio.start(0,99999);

                         

                        _global.fadeSoundF = function(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void

                        {

                           

                                  clearInterval(mc.fadeI);

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

                        if(volumeInc!=0){

                                  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);

                                  }

                        }

                        1 person found this helpful
                        • 9. Re: Function to fade audio smoothly from one level to another in AS2
                          PeteGould Level 1

                          Still misbehaving.  I've re-added trace statements at the entry and exit of fadeSoundF to give the volume of sndAudio.  Typical output is below.

                           

                          One thing I'm also noticing that perhaps wasn't as evident before: in addition to the random levels, it's ignoring the "seconds" argument, i.e., how long the audio level transition should take. These transitions appear to be happening very, very slowly (ten seconds, thirty seconds) even though it's being passed an argument of 1 second.

                           

                          Silly question.  What's "mc.fadeI" in the statement "mc.fadeI=setInterval(fadeF,100,mc,s,volumeInc,vol)" ?

                           

                           

                          Arriving with sound set to level 30

                          Leaving with sound set to level 30

                           

                          Arriving with sound set to level 30

                          Leaving with sound set to level 30

                           

                          Arriving with sound set to level -303

                          Leaving with sound set to level -303

                           

                          Arriving with sound set to level 2

                          Leaving with sound set to level 2

                           

                          Arriving with sound set to level 0

                          Leaving with sound set to level 0

                           

                          Arriving with sound set to level 30

                          Leaving with sound set to level 30

                           

                          Arriving with sound set to level -24

                          Leaving with sound set to level -24

                           

                          Arriving with sound set to level 25

                          Leaving with sound set to level 25

                           

                          Arriving with sound set to level -21

                          Leaving with sound set to level -21

                           

                          Arriving with sound set to level 27

                          Leaving with sound set to level 27

                           

                          Arriving with sound set to level -362

                          Leaving with sound set to level -362

                           

                          Arriving with sound set to level -2

                          Leaving with sound set to level -2

                           

                          Arriving with sound set to level 0

                          Leaving with sound set to level 0

                          • 10. Re: Function to fade audio smoothly from one level to another in AS2
                            kglad Adobe Community Professional & MVP

                            this line:

                             

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

                             

                            should be

                             

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

                            1 person found this helpful
                            • 11. Re: Function to fade audio smoothly from one level to another in AS2
                              PeteGould Level 1

                              THAT was the biggest problem.  Now it's almost 100%.  There's still one tiny issue: when it has muted the audio it can still be heard faintly.  In testing it with additional trace statements, I see the problem: when it's asked to set the audio level to 0 it sets it to 1; when asked to set it to 30 it sets it to 29.

                               

                              Pete

                              • 12. Re: Function to fade audio smoothly from one level to another in AS2
                                PeteGould Level 1

                                I think this modification fixes it once and for all - what do you think?

                                 

                                if (Math.abs(s.getVolume()-endVol)<Math.abs(inc)-1) // the -1 at the end seems to be necessary

                                 

                                Peter

                                • 13. Re: Function to fade audio smoothly from one level to another in AS2
                                  kglad Adobe Community Professional & MVP

                                  no, use:

                                   

                                  this.createEmptyMovieClip("mcMusictrackHolder", this.getNextHighestDepth());

                                  var sndAudio:Sound = new Sound(mcMusictrackHolder);

                                  var nMaxMusicVolume:Number = 30;

                                  //

                                  sndAudio.attachSound("MusicTrack");

                                  sndAudio.setVolume(nMaxMusicVolume);

                                  sndAudio.start(0,99999);

                                   

                                  _global.fadeSoundF = function(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void

                                  {

                                     

                                            clearInterval(mc.fadeI);

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

                                  if(volumeInc!=0){

                                            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)<Math.abs(inc))

                                            {

                                  s.setVolume(endVol);

                                                      clearInterval(mc.fadeI);

                                            }

                                  }

                                  • 14. Re: Function to fade audio smoothly from one level to another in AS2
                                    PeteGould Level 1

                                    I’m hesitant to mark your last response “correct answer” with the typo in it because it’ll drive some future person crazy.  Could you edit the message to fix the typo?  Then I can mark it correct (I don’t know this forum software all that well but I believe your account gets some benefit from my doing that, no?)

                                     

                                    Peter

                                    • 15. Re: Function to fade audio smoothly from one level to another in AS2
                                      kglad Adobe Community Professional & MVP

                                      i don't see a typo in message 13.  do you?