9 Replies Latest reply on Aug 30, 2008 9:20 AM by robdillon

    Setting Sound Volume in the Timeline

    Denatram
      I am using the following code to toggle the sound volume from 0 to 100 using a button. It works fine and collects the value for the variable called soundVolume as I want it to. What I need is a way to set the timeline volume using the variable soundVolume's value when entering the frame. I will be collecting the value for soundVolume from another application on start. I have tried several ways and nothing seems to work. Any suggestions?

      //This variable holds the state of sound volume and passes and collects to/from so
      // the flash files keep the sound level the same from page to page (i.e. the user does
      //not have to keep clicking mute to change the volume state

      var soundVolume:Number;

      //following code sets the new button for when sound is off as visible

      var mute_mc2 = _root.attachMovie("mute_mc2", "mute_mc201", 99);
      mute_mc201._visible = false;
      mute_mc201._x = -193;
      mute_mc201._y = 212.0;


      //following code sets the visibility state of the mute button depending on the variable SoundVolume


      onEnterFrame = function () {
      if (soundVolume == 0) {
      mute_mc201._visible = true;
      mute_mc201._x = 187.9;
      mute_mc201._y = 248.0;
      mute_mc._visible = false;
      } else if (soundVolume == 100) {
      mute_mc201._visible = false;
      mute_mc._visible = true;
      }
      };







      //The following code mutes the sound and sets the sound volume variable

      var allsound:Sound = new Sound(this);


      mute_mc.onRelease = function() {
      if (allsound.getVolume() == 100) {
      allsound.setVolume(0);
      soundVolume = 0;
      } else if (allsound.getVolume() == 0) {
      allsound.setVolume(100);
      soundVolume = 100;
      }
      trace(soundVolume);
      };

      mute_mc2.onRelease = function() {
      if (allsound.getVolume() == 100) {
      allsound.setVolume(0);
      soundVolume = 0;
      } else if (allsound.getVolume() == 0) {
      allsound.setVolume(100);
      soundVolume = 100;
      }
      trace(soundVolume);
      };
        • 1. Re: Setting Sound Volume in the Timeline
          kglad Adobe Community Professional & MVP
          what do you mean by, "the timeline volume"?

          if you mean the volume for a sound attached to your timeline, none of the sounds properties can be controlled by actionscript (except stopping that sound along with all others using stopAllSounds() ).
          • 2. Re: Setting Sound Volume in the Timeline
            Denatram Level 1
            Hmmm.....the code I am showing actually does set the volume of the audio that is in the timeline. The audio is not called using "attachSound" and the audio file in the library is not set to export to actionscript. When you click the button, if the sound volume is currently at 100, it changes to 0. If the sound volume is currently at 0, it changes to 100. It isn't stopping the sound, because everything is staying synched.

            I want to be able to set the volume when the swf file first starts playing based on the value of the variable soundVolume. In other words, to act similar to the button but based on the value of soundVolume.
            • 3. Re: Setting Sound Volume in the Timeline
              Denatram Level 1
              FYI, this code is on frame 1. Sorry if the additional code for setting the visibility of the buttons is distracting.
              • 4. Re: Setting Sound Volume in the Timeline
                kglad Adobe Community Professional & MVP
                you're correct.

                use:

                allsound.setVolume(soundVolume);
                • 5. Re: Setting Sound Volume in the Timeline
                  robdillon Most Valuable Participant
                  The code that you show is creating a sound object named allsound. The two buttons mute_mc and mute_mc2 are toggling the value of the volume property for that sound object between 0 and 100. Ordinarily, as kglad stated, you can't control a sound that is directly attached to a keyframe in the timeline. This is wild sound and will play at whatever volume is set at the keyframe where it begins. The sound object allsound that is instantiated in the code will control any sound in the movie that is not attached to another sound object. While this works, it is not good practice.

                  You could use setVolume() to set an initial value for the sound at the frame where the sound begins to play. Since the sound object is instantiated in the first frame, you can use allsound.setVolume() to any value that you want between 0 and 100 at that keyframe.

                  The variable soundVolume is only used inside the two onRelease functions and has no meaning whatsoever. You don't need it inside the function or outside.

                  That onEnterFrame function is just tying up processor cycles. You can replace all of that code by transferring the visible toggle to the onRelease functions. There is no need to reset the visible of any movieClip 30 times a second.
                  • 6. Re: Setting Sound Volume in the Timeline
                    Denatram Level 1
                    Thanks, Rob. I see what you are saying about moving the onEnterFrame code over to the onRelease code. It would be more efficient. I will try that.

                    I am still working on getting flash to recognize the soundVolume variable when it is passed to the swf file from the other application. I will post if I get it to work.
                    Thanks,
                    Dena
                    • 7. Re: Setting Sound Volume in the Timeline
                      Denatram Level 1
                      Just to let you know, I got it working!! I am so happy!

                      I ended up moving my buttons out of flash and those functions are occuring in the other application. The buttons send set variable commands to flash to adjust the volume. Thanks to kglad and Rob for your suggestions.

                      Here is the new code for frame one:

                      //variable to pass back and forth from external application to control sound on and sound off. Initial volume set to on.

                      var soundVolume:Number;
                      soundVolume = 100;

                      //sets the sound volume on any frame based on the value of soundVolume variable

                      this.onEnterFrame = function () {
                      var allsound:Sound = new Sound(this);
                      allsound.setVolume(soundVolume);
                      }
                      • 8. Re: Setting Sound Volume in the Timeline
                        kglad Adobe Community Professional & MVP
                        you're welcome.
                        • 9. Re: Setting Sound Volume in the Timeline
                          robdillon Most Valuable Participant
                          A couple of things that you might want to consider: If you place that onEnterFrame function in the timeline of your movie, its going to run forever. So, if you place that function at frame 10, and then the playback head moves on, the function is still running over and over and over again. You really only want to set or change the volume once. You can do that from any keyframe, just like calling the function, like this:
                          var allsound:Sound = new Sound(this);
                          allsound.setVolume(soundVolume);
                          at the keyframe where you want to change the volume.

                          Alternately, you could place the volume setting function at the first frame where you are creating the variable:

                          var soundVolume:Number = 100;

                          function changeVolume(newVolume) {
                          var allsound:Sound = new Sound(this);
                          allsound.setVolume(newVolume);
                          }

                          Then you can call the function from anywhere in the movie, even the first frame like this:

                          changeVolume(soundVolume);

                          This will take the value of the soundVolume variable and pass it to the function that will set the new volume to that value.