谁能告诉我为什么我的音量控制不起作用?

发布于 2024-10-10 00:26:53 字数 2781 浏览 0 评论 0原文

谁能告诉我为什么我的音量控制不起作用...?

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();
        }});
        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);
    }
}

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();
        }});
        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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-10-17 00:26:53

音量永远不会改变,只是因为......你永远不会改变它!
我想您希望在滑块状态更改时修改音量,因此您只需在 stateChanged 方法中设置音量即可。为此,您可以使用以下指令:volume.setValue(-20.63f),您已在程序的其他地方使用过该指令。只需将参数替换为您想要的值(例如滑块的值)。

希望这有帮助。

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.

梦醒时光 2024-10-17 00:26:53

好的,看来您正确地控制了“音量”。您是说您在下一行中输入了不同的值,但听起来总是相同的音量?

volume.setValue(-20.63f);  

我无法想象 10 或 80 的值会做什么,因为我认为范围是 -80 到 6。而且大部分底部几乎听不见。它应该对应于从 -80 到 6 的分贝或伏特或更典型的 VU 表。例如,我的调音台有一个范围从 -20 到 +5 的 VU 表。

无论如何,尝试像 5 或 0 或 -10 这样的测试值可能更有可能听起来像某些东西。

此外,大多数人希望在播放过程中改变音量,这是一个动态过程,因此我们正在代码中寻找逻辑位置来执行此操作,例如计时器或滑块。但我猜你只是想让声音以不同的硬编码声音级别播放,而滑块只是用于显示进度。我发现代码以及问题的描述都很难破译。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文