Can anyone tell me why my volume control doesn't work?
Can anyone tell me why my volume control doesn't work...?
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import javax.swing.event.*;
/*<Applet code="kkk" height=400 width=400></Applet>*/
public class kkk extends JComponent
{
static File f1;
int prog;
static JFrame jf;
int al;
JLabel time;
Timer tr;
Button b;
int pos=0;
Clip c;
AudioInputStream a;
JSlider s;
public static void main(String args[])
{
f1=new File("mm.wav");
jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
kkk kp=new kkk(f1);
jf.getContentPane().add(kp, "Center");
jf.setSize(400,400);
jf.setVisible(true);
}
kkk(File f1)
{
try
{
a=AudioSystem.getAudioInputStream(f1);
AudioFormat af=a.getFormat();
DataLine.Info di=new DataLine.Info(Clip.class,af);
c=(Clip)AudioSystem.getLine(di);
c.open(a);
}
catch(Exception e)
{
System.out.println("Exception caught ");
}
finally
{
try
{
a.close();
}
catch(Exception e)
{
System.out.println("Exception caught ");
}
}
al=(int)(c.getMicrosecondLength()/1000);
s=new JSlider();
Button b=new Button("play");
time=new JLabel();
Box row = Box.createHorizontalBox();
row.add(s);
row.add(b);
row.add(time);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent eee)
{
play();
}});
开发者_如何学Python s.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent ee)
{
//repaint();
prog=s.getValue();
time.setText(prog / 1000 + "." + (prog % 1000) / 100);
//if(prog!=ap)
//skip(prog);
}});
tr = new javax.swing.Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
tick();
}
});
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(row);
}
public void play()
{
try
{
FloatControl volume = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(-20.63f);
}
catch(Exception e)
{}
c.start();
tr.start();
}
//public void skip(
public void tick()
{
pos = (int) (c.getMicrosecondPosition() / 1000);
s.setValue(pos);
}
}
The volume never changes just because...you're never changing it ! I suppose you want the volume to be modified when the slider state changes, so you just have to set the volume in your stateChanged method. In order to do this, you can use the following instruction: volume.setValue(-20.63f) that you have already used elsewhere in your program. Just replace the parameter by the value you want (e.g. the value of the slider).
Hope this helps.
OK, it seems like you made the "volume" control correctly. Are you saying you put different values in the following line and it always sounded the same volume?
volume.setValue(-20.63f);
I can't imagine what values of 10 or 80 would do, as I think the range is -80 to 6. And most of the bottom end of that is pretty much inaudible. It's supposed to correspond to decibels from -80 to 6 or volts or something more typical to a VU meter. My mixer has a VU meter that ranges from -20 to +5, for example.
In any event, trying test values like 5 or 0 or -10 might have a better chance of sounding like something.
Also, most people expect that one changes the volume during playback, as a dynamic process, and so we are looking for logical places in your code to do this, such as the Timer or the Slider. But I guess you were just trying to get sounds to play at different hard-coded sound levels, and the slider was just for displaying progress. I found the code rather hard to decipher, as well as the description of the problem.
精彩评论