如何在 Java Sound API 中正确使用 FloatControl? [英] How do I correctly use FloatControl with the Java Sound API?

查看:47
本文介绍了如何在 Java Sound API 中正确使用 FloatControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用 Java 编写一个测试程序,我希望能够在 GUI 应用程序上使用 JSlider 来控制我计算机的主 SPEAKER.我已经阅读了关于 AudioSystem 类的 this 页面和 控制页面.我的理解是我需要通过混音器获得一个端口或线路,然后在该线路或端口上建立一个控制.由于我正在控制音量,因此我需要实现一个 FloatControl.我的程序编译没有问题,但是当我运行它并调整 JSlider 时,我收到了 catch 子句下的错误消息.

I am currently writing a test program in Java that I want to be able to control my computer's main SPEAKER with a JSlider on a GUI application. I have read through this page on the AudioSystem class and this page on Controls. It is my understanding that I need to get a Port or line through a mixer and then institute a Control on that line or port. Since I am controlling volume I need to implement a FloatControl. My program compiles with no problem, however when I run it and adjust the JSlider I get sent the error message under the catch clause.

这是我的测试程序:

import java.awt.BorderLayout;
import java.io.IOException;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Port;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class speakerWork2
{
    public static void main(String [] args)throws IOException
    {
        Frame speaker = new Frame();
    }
}

class Frame extends JFrame
{
    JSlider mainVolume = new JSlider(JSlider.VERTICAL,0,100,50);

    public Frame()
    {
        super();
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setSize(100, 450);
        setLocation(300,200);

        add(mainVolume);

        mainVolume.addChangeListener(new mainVolumeHandler());
    }

    public class mainVolumeHandler implements ChangeListener
    {
        int sliderVal = mainVolume.getValue();

        public void setValue(FloatControl sliderVal) 
        {
        }

        public void stateChanged(ChangeEvent f)
        {
            Port SPEAKER;  
            FloatControl volCtrl;  
            try 
            {  
                Mixer mixer = AudioSystem.getMixer(null);  
                SPEAKER = (Port)mixer.getLine(Port.Info.SPEAKER);  
                SPEAKER.open();  
                volCtrl = (FloatControl) SPEAKER.getControl(  
                        FloatControl.Type.VOLUME);  
            } 
            catch (Exception e) 
            {  
                System.out.println("Failed trying to find SPEAKER"  
                        + " VOLUME control: exception = " + e);  
            }
        }
    }
}

当我运行上述程序时,打印出异常 e:

When I run the above program, exception e prints:

java.lang.IllegalArgumentException: Line unsupported: SPEAKER target port

当我在我的计算机上查询混音器、线路和端口时,我的扬声器得到以下结果...

When I query my computer for mixers, lines and ports I get the following result for my Speakers...

mixer name: Port Speakers / Headphones (IDT High Definition Audio CODEC)
Line.Info: SPEAKER target port
volCtrl.getValue() = 1.0

我做错了什么,我需要做些什么来纠正自己?

What am I doing wrong, and what do I need to do to correct myself?

推荐答案

我能想到两个可能需要调查的问题:

I can think of two plausible issues to investigate:

(1) 如果您的目标扬声器端口出现在特定混音器上,如果它不是默认混音器,您必须明确打开该混音器.您当前正在打开默认"混音器.

(1) If your target speaker port appears on a specific mixer, you have to explicitly open that mixer if it is not the default mixer. You are currently opening the "default" mixer.

(2) 如果音频资源正被另一个程序使用,它可能不可用.我听说 Linux 比其他系统更容易发生这种情况.

(2) If the audio resource is being used by another program, it may not be available. I've heard of this happening more with Linux than other systems.

这篇关于如何在 Java Sound API 中正确使用 FloatControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆