7

I'm trying to make an mp3 player in java and I can`t figure out how to control the volume in it.

I've tried something like this:

         // Adjust the volume on the output line.
         if (dataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
             FloatControl volume = (FloatControl) dataLine.getControl(FloatControl.Type.MASTER_GAIN);
            volume.setValue(100.0F);
         }

Everything I have written until this code worked fine but apparently the dataLine is NOT control Supported because it jumps over this IF statement.

My question is this : Do you have any idea why this is happening and how could I work this issue around so that I could control the volume of my application?

Alex I
  • 19,689
  • 9
  • 86
  • 158
Liviu
  • 149
  • 1
  • 8

3 Answers3

4

OK GUYS,

I found my mess-up. I actually forgot to call the dataLine.open(audioFormat) function which acquires the system resources.

So the code workes just fine, in case anyone has this kind of problems too

Liviu
  • 149
  • 1
  • 8
3

Have you tried to see what dataLine.getControls() will return ?

Obtains the set of controls associated with this line. Some controls may only be available when the line is open. If there are no controls, this method returns an array of length 0.

If you want volume wouldn't you want to test for the FloatControl.Type.VOLUME control ?

Mark
  • 16,772
  • 9
  • 42
  • 55
  • dataLine.getControls() returns this Master Gain with current value: 0.0 dB (range: -80.0 - 6.0206) Mute Control with current value: False Balance with current value: 0.0 (range: -1.0 - 1.0) Pan with current value: 0.0 (range: -1.0 - 1.0) I`ve been messing with my code in the meantime and realized that the Master Gain`s values IS actually changed but I can`t hear any cahnges in the song – Liviu Jan 12 '11 at 13:23
1
 float vol=50;
final FloatControl volumeControl = (FloatControl) auline.getControl( FloatControl.Type.MASTER_GAIN );
volumeControl.setValue( 20.0f * (float) Math.log10( vol / 100.0 ) );

vol=0 means mute.