Excuse me if I'm starting up yet another volume control question. I just didn't want to hijack the thread that's currently open. My question deals more with where the volume control code should be located in the class. The current location seems to work, but I was wondering if there was a better programming practice.
I currently have a class specifically dedicated to the GUI. In there, I have a jSlider that controls the volume of the audio that's being played. Here's the portion of code:
private void jSliderVolumeBTStateChanged(javax.swing.event.ChangeEvent evt) {
audioPlayer.setVolume((float)jSliderVolumeBT.getValue());
System.out.println("new JSlider state: " + (float)jSliderVolumeBT.getValue());
}
When a change event is triggered, the
setVolume method in the
audioPlayer class is called.
private float sliderVolume = 50.0F;
public void setVolume(float sliderVolume){
this.sliderVolume = sliderVolume;
}
Now the portion of the code that controls the line volume is located in the play audio thread:
class PlayThread extends Thread{
byte tempBuffer[] = new byte[20000];
public void run(){
try{
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int nBytesRead;
while((nBytesRead = audioInputStream.read(tempBuffer,0,tempBuffer.length)) != -1 && stopPlayback == false){
if (sourceDataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl volumeControl = (FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
float max_slider_value = 100F;
float volume = (sliderVolume/max_slider_value);
float dB = (float)(Math.log(volume == 0.0F ? 0.0001F : volume)/Math.log(10.0)*20.0);
volumeControl.setValue(dB);
}
if(nBytesRead > 0){
sourceDataLine.write(tempBuffer, 0, nBytesRead);
}
}
sourceDataLine.drain();
sourceDataLine.close();
stopPlayback = false;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
+(I tried using FloatControl.Type.VOLUME with no success. I know
captfoss posted a solution to this in another thread, but I still can't get it to work.)+
Is there a better location for me to place the volume control part of the code?
Thanks in advance.