9 Replies Latest reply on Feb 20, 2007 8:39 AM by

    Sound Fade onEnterFrame

      Please help!
      I'm trying desparately to get the audio to fade over a 3 second period when the movie gets to a specific frame near last few frames of my movie but haven't been able to find any code that works for me. I've seen several that work for button but haven't been able to convert the code for use in a frame based execution.




      Thanks
      Dave

      This is the code I started with:

        • 1. Re: Sound Fade onEnterFrame
          Level 7
          Dave,

          > I'm trying desparately to get the audio to fade over a 3
          > second period when the movie gets to a specific frame
          > near last few frames of my movie

          If you want to fade over seconds, you might rather want to go with a
          mechanism like setInterval(), which bases itself on milliseconds.
          onEnterFrame is based on framerate, which ... well, which depends on your
          framerate. ;) And framerates are arbitrary.

          > this.onEnterFrame = function(){

          So far, so good -- with the caveat that this function will occur at a
          rate of time consistent with your framerate. This may or may not take three
          seconds.

          > so = audio;

          Not sure why so is necessary, if you already have an audio reference.

          > rate = 5;
          > newVolume = so.getVolume() + rate;
          > if(newVolume < 100) {
          > so.setVolume(newVolume);

          This can be simplified a bit (more in a moment).

          > } else {
          > so.setVolume(100);
          > this.onEnterFrame = null;
          > }
          > }

          This isn't too far off. Here's an approach I might take.

          // Assuming an audio variable already ...

          // Rather than constantly checking audio.getVolume(),
          // set an arbitrarily named vol variable to zero, since
          // we're fading in ...

          var vol:Number = 0;

          this.onEnterFrame = function():Void {
          if (vol < 100) {
          vol += 5;
          audio.setVolume(vol);
          } else {
          audio.setVolume(100);
          delete this.onEnterFrame;
          }
          }


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          1 person found this helpful
          • 2. Re: Sound Fade onEnterFrame
            Thanks Dave
            I played with the code you sent me and tweaked it a little bit and ended up with this:

            var vol:Number = 50;

            this.onEnterFrame = function():Void {
            if (vol > 1) {
            vol -= 1;
            audio.setVolume(vol);
            } else {
            audio.setVolume(0);
            delete this.onEnterFrame;
            }
            }

            I ended up setting the vol to 50 because that is where the volume is in the movie. I tried using the getVolume(); to detemine the volume but that didn't seem to work.

            Anyway thanks for the help.
            • 3. Re: Sound Fade onEnterFrame
              Level 7
              dmschenk,

              > Thanks Dave

              Sure thing.

              > I played with the code you sent me and tweaked it a little
              > bit and ended up with this:
              >
              > var vol:Number = 50;

              Okay.

              > this.onEnterFrame = function():Void {
              > if (vol > 1) {
              > vol -= 1;
              > audio.setVolume(vol);
              > } else {
              > audio.setVolume(0);
              > delete this.onEnterFrame;
              > }
              > }

              Sure, that makes sense. :)

              > I ended up setting the vol to 50 because that is where the
              > volume is in the movie. I tried using the getVolume(); to
              > detemine the volume but that didn't seem to work.

              It sure should. But I wonder if you simply invoked getVolume() on its
              own, as if it were a function? Here's the thing about objects ... objects
              are defined by something called classes. Classes determine a given object's
              properties (characteristics), things it can do (methods), and things it can
              react to (events). The Sound class sets apart sound objects from, say,
              movie clip objects, because the Sound class defines different properties,
              methods, and events from the MovieClip class.

              By the time you're dealing with a given object, you have to invoke a
              method *on* that object instance. e.g.

              var vol:Number = audio.getVolume();

              ... assuming, by that point, that audio is already a valid Sound instance.

              Of course, maybe you already know all this, so I apologize if I'm
              telling you thinks you already know. You should be able to trace() any
              property or method at any time to kinda see what you're dealing with ...

              trace(audio.getVolume());

              ... which helps you see "under the hood," a bit.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              1 person found this helpful
              • 4. Re: Sound Fade onEnterFrame
                You were right I did try to use the getVolume by itself so I'll try what you are showing me. I don't know that audio is a valid sound instance although the volume does fade as it should.

                I'm really new to actionscript so I appreciate all the tips... like the trace statement! I do need to try that one more!!

                Dave
                Thanks for your help

                Dave
                • 5. Re: Sound Fade onEnterFrame
                  Level 7
                  Dave,

                  > You were right I did try to use the getVolume by itself so I'll
                  > try what you are showing me. I don't know that audio is a
                  > valid sound instance although the volume does fade as it should.

                  If the volume does fade correctly, then I can only assume audio is a
                  valid Sound instance. That means somewhere along the line, you must have
                  written: var audio = new Sound(), or showhow set audio to an existing Sound
                  instance.

                  > I'm really new to actionscript so I appreciate all the tips... like
                  > the trace statement! I do need to try that one more!!

                  That's one of many debugging tools native to Flash. See this article
                  for a bunch more. :)

                  http://www.quip.net/blog/2006/flash/debugging-actionscript


                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."


                  • 6. Re: Sound Fade onEnterFrame
                    Not quite.... I had added a pre-built mp3 player (SoundPlayer from flashloaded)
                    then gave the player an instance name of audio. I couldn't find anywhere in its code where it set the instance name of the audio file other than a line of code that said "this.currentPlay = new Sound(this);" so maybe me giving the player itself was good enough for a portion of the code to work but not all of it.

                    var vol:Number = audio.getVolume(); didn't work and neither did the trace(audio.getVolume()); but I'm assuming that is because I don't really have the correct instance name or path.

                    Dave

                    >If the volume does fade correctly, then I can only assume audio is a
                    >valid Sound instance. That means somewhere along the line, you must have
                    >written: var audio = new Sound(), or showhow set audio to an existing Sound
                    >instance.
                    • 7. Re: Sound Fade onEnterFrame
                      Level 7
                      Dave,

                      > Not quite.... I had added a pre-built mp3 player (SoundPlayer
                      > from flashloaded) then gave the player an instance name of
                      > audio.

                      Aha! Okay. :) Then you're dealing with a custom SoundPlayer object,
                      and your audio variable is an instance of that object.

                      > I couldn't find anywhere in its code where it set the instance
                      > name of the audio file other than a line of code that said
                      > "this.currentPlay = new Sound(this);"

                      Ah, good! Well, that means your SoundPlayer object -- that is, this
                      pre-built mp3 player -- has a currentPlay property, and this
                      currentPlayproperty is a Sound instance.

                      > var vol:Number = audio.getVolume(); didn't work and neither
                      > did the trace(audio.getVolume()); but I'm assuming that is because
                      > I don't really have the correct instance name or path.

                      Well, you do -- but your instance name refers to a SoundPlayer instance,
                      and whoever wrote this object chose not to provide it a getVolume() method.
                      But ... according to that line above, this SoundPlayer object has a
                      currentPlay property that *is* a Sound instance. So, it may be possible to
                      get to the volume like this:

                      trace(audio.currentPlay.getVolume());

                      See what's going on? audio is your SoundPlayer reference; SoundPlayer
                      has that currentPlay property, so audio.currentPlay is a valid reference.
                      Then, currentPlay refers to a Sound instance, and the Sound class features a
                      getVolume() method ... so the full path shown above should do it.


                      David Stiller
                      Adobe Community Expert
                      Dev blog, http://www.quip.net/blog/
                      "Luck is the residue of good design."


                      1 person found this helpful
                      • 8. Re: Sound Fade onEnterFrame
                        Sweet!! That worked great!
                        So this was the final code configuation:

                        var vol:Number = audio.currentPlay.getVolume();
                        // trace(audio.currentPlay.getVolume());
                        this.onEnterFrame = function():Void {
                        if (vol > 1) {
                        vol -= 1;
                        audio.setVolume(vol);
                        } else {
                        audio.setVolume(0);
                        delete this.onEnterFrame;
                        }
                        }

                        Thanks for all the help!!!

                        Dave
                        • 9. Re: Sound Fade onEnterFrame
                          Sweet!! That worked great!
                          So this was the final code configuation:

                          var vol:Number = audio.currentPlay.getVolume();
                          // trace(audio.currentPlay.getVolume());
                          this.onEnterFrame = function():Void {
                          if (vol > 1) {
                          vol -= 1;
                          audio.setVolume(vol);
                          } else {
                          audio.setVolume(0);
                          delete this.onEnterFrame;
                          }
                          }

                          Thanks for all the help!!!

                          Dave