5 Replies Latest reply: Aug 10, 2010 2:31 PM by captfoss RSS

    Volume control.

    843802
      Hi.
      I want to be able to control the volume in my little music player. I have already made it so I can control the master volume of the whole system but I want to be able to only control the volume for the actual music player. So if I am playing a game or something and I put the music volume control button down then the music is only affected, not any other sound that is playing in other applications.
      Can somebody point me in the right direction?
      This is the class I use:
      import javax.sound.sampled.Mixer;
      import javax.sound.sampled.Mixer.Info;
      import javax.sound.sampled.Line;
      import javax.sound.sampled.Port;
      import javax.sound.sampled.AudioSystem;
      import javax.sound.sampled.FloatControl;
      
      public class VolumeControl {
      
          private Port lineOut;
          private FloatControl volControl;
          private Mixer mixer; 
      
          public VolumeControl() {
              lineOut = null;
              volControl = null;
      
              //It gets everyone of the System's Mixers
              Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
              try{
                  //it Looks for those Mixers that suport the OutPut SPEAKER
                  for (int i =0; i < mixerInfo.length; i++){
                      mixer = AudioSystem.getMixer( mixerInfo[i] );
      
                      //If the SPEAKER is supported, then it gets a line
                      if ( mixer.isLineSupported( Port.Info.SPEAKER ) ){
                          lineOut = (Port) mixer.getLine( Port.Info.SPEAKER );
      
                          lineOut.open();
      
                          //Once we have the line, we request the Volumen control as a FloatControl
                          volControl = (FloatControl) lineOut.getControl(FloatControl.Type.VOLUME);
                          //Everything is done
                      }
                  }
              }
              catch(Exception error){
                  error.printStackTrace();
              }
          }
      
          public float getValue() {
              return volControl.getValue();
          }
      
          public void setValue(float value) {
              volControl.setValue( value );
          }
      
          public boolean isControlValid() {
              return (volControl == null) ? false : true;
          }
      
          public void close() {
              lineOut.close();
          }
      } 
        • 1. Re: Volume control.
          captfoss
          [http://www.vsj.co.uk/java/display.asp?id=370]

          Read that article. Specifically, the "sing along recorder" has an example of obtaining and using a volume control...
          • 2. Re: Volume control.
            843802
            Okey. Thanks.
            I have read over that article but I can't quiet understand how he is doing it. He seems to be adjusting the volume at which he is recording. There is only one volume control for that.
            What I do in my class up there in the first post is to adjust the master control. But I want only to adjust the volume on the line I am holding in my program.
            Is it possible that you have a simpler example? By the way, I am using JLayer " http://www.javazoom.net/javalayer/javalayer.html ". He is using the Javasound.
            If I could find a solution without using JLayer then maybe I would change to the Javasound.

            Thanks in advance.
            • 3. Re: Volume control.
              captfoss
              By the way, I am using JLayer " http://www.javazoom.net/javalayer/javalayer.html ". He is using the Javasound.
              Perhaps you missed the part where this was a Java Sound forum and you said nothing about JLayer to begin with? That's probably why I gave you a link to JavaSound example code...
              I have read over that article but I can't quiet understand how he is doing it. He seems to be adjusting the volume at which he is recording. There is only one volume control for that.
              He's obtaining the VolumeControl on the PortMixer object by iterating through the controls available on the Port until he finds one that is a volume control...
              Is it possible that you have a simpler example?
              That example is pretty simple, so no...
              What I do in my class up there in the first post is to adjust the master control. But I want only to adjust the volume on the line I am holding in my program.
              You obtained the Volume control on the system mixer. You want to obtain the volume control on the input to the system mixer...but considering you didn't post that code, I can't tell you anything more specific than that.
              • 4. Re: Volume control.
                843802
                Sigh...
                Okey.
                One more thing then. On this website that you showed me he talks about "You can find the source code to this recorder in the KokRecorder.java file of the source distribution.".
                Do you know where I can find the full code example?
                • 5. Re: Volume control.
                  captfoss
                  Simmisj wrote:
                  Do you know where I can find the full code example?
                  Nope. Sorry...