列出Applet中的输入和输出音频设备

发布于 2024-12-13 07:08:58 字数 3158 浏览 2 评论 0原文

我正在运行一个签名的小程序,它需要为用户提供选择输入和输出音频设备的能力(类似于 Skype 提供的功能)。

我从其他 线程

import javax.sound.sampled.*;
public class SoundAudit {
  public static void main(String[] args) { try {
    System.out.println("OS: "+System.getProperty("os.name")+" "+
      System.getProperty("os.version")+"/"+
      System.getProperty("os.arch")+"\nJava: "+
      System.getProperty("java.version")+" ("+
      System.getProperty("java.vendor")+")\n");
      for (Mixer.Info thisMixerInfo : AudioSystem.getMixerInfo()) {
        System.out.println("Mixer: "+thisMixerInfo.getDescription()+
          " ["+thisMixerInfo.getName()+"]");
        Mixer thisMixer = AudioSystem.getMixer(thisMixerInfo);
        for (Line.Info thisLineInfo:thisMixer.getSourceLineInfo()) {
            if (thisLineInfo.getLineClass().getName().equals(
              "javax.sound.sampled.Port")) {
              Line thisLine = thisMixer.getLine(thisLineInfo);
              thisLine.open();
              System.out.println("  Source Port: "
                +thisLineInfo.toString());
              for (Control thisControl : thisLine.getControls()) {
                System.out.println(AnalyzeControl(thisControl));}
              thisLine.close();}}
        for (Line.Info thisLineInfo:thisMixer.getTargetLineInfo()) {
          if (thisLineInfo.getLineClass().getName().equals(
            "javax.sound.sampled.Port")) {
            Line thisLine = thisMixer.getLine(thisLineInfo);
            thisLine.open();
            System.out.println("  Target Port: "
              +thisLineInfo.toString());
            for (Control thisControl : thisLine.getControls()) {
              System.out.println(AnalyzeControl(thisControl));}
            thisLine.close();}}}
  } catch (Exception e) {e.printStackTrace();}}
  public static String AnalyzeControl(Control thisControl) {
    String type = thisControl.getType().toString();
    if (thisControl instanceof BooleanControl) {
      return "    Control: "+type+" (boolean)"; }
    if (thisControl instanceof CompoundControl) {
      System.out.println("    Control: "+type+
        " (compound - values below)");
      String toReturn = "";
      for (Control children:
        ((CompoundControl)thisControl).getMemberControls()) {
        toReturn+="  "+AnalyzeControl(children)+"\n";}
      return toReturn.substring(0, toReturn.length()-1);}
    if (thisControl instanceof EnumControl) {
      return "    Control:"+type+" (enum: "+thisControl.toString()+")";}
    if (thisControl instanceof FloatControl) {
      return "    Control: "+type+" (float: from "+
        ((FloatControl) thisControl).getMinimum()+" to "+
        ((FloatControl) thisControl).getMaximum()+")";}
    return "    Control: unknown type";}
}

但我得到的是:

Mixer: Software mixer and synthesizer [Java Sound Audio Engine]
Mixer: No details available [Microphone (Pink Front)]

我期待得到我的设备的真实列表(我的首选项面板显示 3 个输出设备和 1 个麦克风)。我正在 Mac OS X 10.6.7 上运行。

还有其他方法可以从 Java 获取该信息吗?

I am running a signed applet that needs to provide the ability for the user to select the input and output audio devices ( similar to what skype provides).

I borrowed the following code from other thread:

import javax.sound.sampled.*;
public class SoundAudit {
  public static void main(String[] args) { try {
    System.out.println("OS: "+System.getProperty("os.name")+" "+
      System.getProperty("os.version")+"/"+
      System.getProperty("os.arch")+"\nJava: "+
      System.getProperty("java.version")+" ("+
      System.getProperty("java.vendor")+")\n");
      for (Mixer.Info thisMixerInfo : AudioSystem.getMixerInfo()) {
        System.out.println("Mixer: "+thisMixerInfo.getDescription()+
          " ["+thisMixerInfo.getName()+"]");
        Mixer thisMixer = AudioSystem.getMixer(thisMixerInfo);
        for (Line.Info thisLineInfo:thisMixer.getSourceLineInfo()) {
            if (thisLineInfo.getLineClass().getName().equals(
              "javax.sound.sampled.Port")) {
              Line thisLine = thisMixer.getLine(thisLineInfo);
              thisLine.open();
              System.out.println("  Source Port: "
                +thisLineInfo.toString());
              for (Control thisControl : thisLine.getControls()) {
                System.out.println(AnalyzeControl(thisControl));}
              thisLine.close();}}
        for (Line.Info thisLineInfo:thisMixer.getTargetLineInfo()) {
          if (thisLineInfo.getLineClass().getName().equals(
            "javax.sound.sampled.Port")) {
            Line thisLine = thisMixer.getLine(thisLineInfo);
            thisLine.open();
            System.out.println("  Target Port: "
              +thisLineInfo.toString());
            for (Control thisControl : thisLine.getControls()) {
              System.out.println(AnalyzeControl(thisControl));}
            thisLine.close();}}}
  } catch (Exception e) {e.printStackTrace();}}
  public static String AnalyzeControl(Control thisControl) {
    String type = thisControl.getType().toString();
    if (thisControl instanceof BooleanControl) {
      return "    Control: "+type+" (boolean)"; }
    if (thisControl instanceof CompoundControl) {
      System.out.println("    Control: "+type+
        " (compound - values below)");
      String toReturn = "";
      for (Control children:
        ((CompoundControl)thisControl).getMemberControls()) {
        toReturn+="  "+AnalyzeControl(children)+"\n";}
      return toReturn.substring(0, toReturn.length()-1);}
    if (thisControl instanceof EnumControl) {
      return "    Control:"+type+" (enum: "+thisControl.toString()+")";}
    if (thisControl instanceof FloatControl) {
      return "    Control: "+type+" (float: from "+
        ((FloatControl) thisControl).getMinimum()+" to "+
        ((FloatControl) thisControl).getMaximum()+")";}
    return "    Control: unknown type";}
}

But what I get:

Mixer: Software mixer and synthesizer [Java Sound Audio Engine]
Mixer: No details available [Microphone (Pink Front)]

I was expecting the get the real list of my devices (My preferences panels shows 3 output devices and 1 Microphone). I am running on Mac OS X 10.6.7.

Is there other way to get that info from Java?

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

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

发布评论

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

评论(3

爱她像谁 2024-12-20 07:08:58

多年来,OS X 的 Java 实现一直是一个非常不幸的限制,顺便说一句,该平台特有的“Java Sound Audio Engine”是唯一以编程方式可用的输出音频线。因此,无论您发送到此行的是什么,即从您创建的任何 java 应用程序发送的内容,都将始终被路由到 OS X 中设置为默认输出的内容,通常是内部扬声器。所以 JSAE 只是“默认音频输出”的 Java 术语。据我们了解 - 可悲的是 - 最新版本仍然是这种情况。

为什么不幸?因为它甚至可以有效地禁用不起眼的音频路由。我们每天都在处理这些问题,这需要增加各种复杂性。有一些解决方法,但需要通过 SoundFlower 和 HiJack Pro 等第三方应用程序。例如 www.soundPimp.com。

For years it has been a very unfortunate limitation of the Java implementation for OS X, being BTW particular for that platform, that "Java Sound Audio Engine" is the only programmatically available output audio line. In consequence, whatever you send to this line, i.e. out from any java application that you make, will always be routed to what has been set as the default output in the OS X, typically internal speakers. So the JSAE is just Java terminology for "default audio out". To our understanding - sadly - this is still the case with latest release.

Why unfortunate ? Because it effectively disables even humble audio routing. We are working with these matters on a daily basis, and it calls for all sorts of added complexity. There are work arounds, but via third party apps as SoundFlower and HiJack Pro. www.soundPimp.com for example.

桃扇骨 2024-12-20 07:08:58

也许你可以修改并使用它。以下代码用于在我的两个小程序上选择音频输出设备。我只对输出线感兴趣,而不是端口或输入线。第一部分列出了菜单栏下拉列表中选项组中的选项。第二部分根据所选选项设置混合器变量。

private void createMenuBars(){
    JMenuBar menuBar = new JMenuBar();
    menuBar.setBounds(0, 0, 60, 20);

    JMenu optionMenu = new JMenu("Options");
    JMenuItem pickMixers = new JMenuItem("Select a Playback Path");
    optionMenu.add(pickMixers);
    optionMenu.addSeparator();

    ButtonGroup mixerSelections = new ButtonGroup();

    addMixerOption("default sound system", mixerSelections, optionMenu, true);

    AudioFormat audioFmt = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
        44100, 16, 2, 4, 44100, false);
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info info : mixers) 
    {
        Mixer mixer = AudioSystem.getMixer(info);

        try
        {
//          System.out.println(info);
            Info sdlLineInfo = new DataLine.Info(SourceDataLine.class, audioFmt);

            // test if line is assignable
            @SuppressWarnings("unused")
            SourceDataLine sdl = (SourceDataLine) mixer.getLine(sdlLineInfo);

            // if successful, add to list
            addMixerOption(info.getName() + " <> " + info.getDescription(),
                mixerSelections, optionMenu, false);
        }
        catch (LineUnavailableException e) 
        {
            //e.printStackTrace();
            System.out.println("Mixer rejected, Line Unavailable: " + info);
        }
        catch (IllegalArgumentException e)
        {
            //e.printStackTrace();
            System.out.println("Mixer rejected, Illegal Argument: " + info);
        }           
    }

    menuBar.add(optionMenu);
    add(menuBar,0);
}

private void addMixerOption(String optionName, ButtonGroup bg, 
    JMenu menu, boolean isSelected
{
    JRadioButtonMenuItem newOption = new JRadioButtonMenuItem(optionName);
    bg.add(newOption);
    newOption.setSelected(isSelected);
    menu.add(newOption);
    newOption.addActionListener(new OptionListener());
    newOption.setActionCommand(optionName); 
}

这是选择选项时设置混合器变量的情况。

public class OptionListener implements ActionListener 
{
    @Override
    public void actionPerformed(ActionEvent arg0) 
    {
        String optionName = arg0.getActionCommand();
        Boolean defaultMixer = true;

        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        for (Mixer.Info info : mixers)
        {
            if (optionName.equals(info.getName()+" <> "+info.getDescription()))
            {
                System.out.println("Option selected >  " + info.getName());
                System.out.println("    description >  " + info.getDescription());
                System.out.println("          class >  " + info.getClass());

                appMixer = AudioSystem.getMixer(info);
                System.out.println(appMixer);
                defaultMixer = false;
            }
        }
        if (defaultMixer) 
        {
            System.out.println("Using default mixer, whatever that is...");
            appMixer = null;
        }
    }
}

控制台消息过多。
我在 http://hexara.com/VSL/JTheremin.htm 中使用它

Maybe you can modify and use this. The following code is used to select audio output devices on two of my Applets. I was only interested in output lines, not ports or input lines. The first part lists the options in an option group in a Menubar dropdown. The second part sets a mixer variable based upon the selected option.

private void createMenuBars(){
    JMenuBar menuBar = new JMenuBar();
    menuBar.setBounds(0, 0, 60, 20);

    JMenu optionMenu = new JMenu("Options");
    JMenuItem pickMixers = new JMenuItem("Select a Playback Path");
    optionMenu.add(pickMixers);
    optionMenu.addSeparator();

    ButtonGroup mixerSelections = new ButtonGroup();

    addMixerOption("default sound system", mixerSelections, optionMenu, true);

    AudioFormat audioFmt = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
        44100, 16, 2, 4, 44100, false);
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info info : mixers) 
    {
        Mixer mixer = AudioSystem.getMixer(info);

        try
        {
//          System.out.println(info);
            Info sdlLineInfo = new DataLine.Info(SourceDataLine.class, audioFmt);

            // test if line is assignable
            @SuppressWarnings("unused")
            SourceDataLine sdl = (SourceDataLine) mixer.getLine(sdlLineInfo);

            // if successful, add to list
            addMixerOption(info.getName() + " <> " + info.getDescription(),
                mixerSelections, optionMenu, false);
        }
        catch (LineUnavailableException e) 
        {
            //e.printStackTrace();
            System.out.println("Mixer rejected, Line Unavailable: " + info);
        }
        catch (IllegalArgumentException e)
        {
            //e.printStackTrace();
            System.out.println("Mixer rejected, Illegal Argument: " + info);
        }           
    }

    menuBar.add(optionMenu);
    add(menuBar,0);
}

private void addMixerOption(String optionName, ButtonGroup bg, 
    JMenu menu, boolean isSelected
{
    JRadioButtonMenuItem newOption = new JRadioButtonMenuItem(optionName);
    bg.add(newOption);
    newOption.setSelected(isSelected);
    menu.add(newOption);
    newOption.addActionListener(new OptionListener());
    newOption.setActionCommand(optionName); 
}

Here is were the mixer variable gets set when an option is selected.

public class OptionListener implements ActionListener 
{
    @Override
    public void actionPerformed(ActionEvent arg0) 
    {
        String optionName = arg0.getActionCommand();
        Boolean defaultMixer = true;

        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        for (Mixer.Info info : mixers)
        {
            if (optionName.equals(info.getName()+" <> "+info.getDescription()))
            {
                System.out.println("Option selected >  " + info.getName());
                System.out.println("    description >  " + info.getDescription());
                System.out.println("          class >  " + info.getClass());

                appMixer = AudioSystem.getMixer(info);
                System.out.println(appMixer);
                defaultMixer = false;
            }
        }
        if (defaultMixer) 
        {
            System.out.println("Using default mixer, whatever that is...");
            appMixer = null;
        }
    }
}

There's an overabundance of console messages.
I use this in http://hexara.com/VSL/JTheremin.htm

寄居者 2024-12-20 07:08:58

这可能是因为 JVM 不支持在 OS X 上检索此信息,或者您的设备可能不受支持。我会做两件事:

  • 尝试使用不同的 JVM
  • 尝试不同的操作系统

我在 Linux 上运行代码并且我正确地获得了所有详细信息:

OS: Linux 2.6.38-12-generic/amd64
Java: 1.6.0_22 (Sun Microsystems Inc.)

Mixer: the ear-candy mixer [PulseAudio Mixer]
Mixer: Direct Audio Device: default, default, default [default [default]]
Mixer: Direct Audio Device: HDA Intel, ALC662 rev1 Analog, ALC662 rev1 Analog [Intel [plughw:0,0]]
Mixer: Direct Audio Device: Plantronics Headset, USB Audio, USB Audio [Headset [plughw:1,0]]
Mixer: Direct Audio Device: USB Device 0x46d:0x8b2, USB Audio, USB Audio [U0x46d0x8b2 [plughw:2,0]]
Mixer: HDA Intel, Realtek ALC662 rev1 [Port Intel [hw:0]]
  Source Port: Mic Boost source port
    Control: Mic Boost (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
  Source Port: Capture source port
    Control: Capture (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Select (boolean)
  Source Port: Capture source port
    Control: Capture (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Select (boolean)
  Target Port: Master target port
    Control: Master (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Headphone target port
    Control: Headphone (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Speaker target port
    Control: Speaker (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: PCM target port
    Control: PCM (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Line target port
    Control: Line (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Mic target port
    Control: Mic (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Mic Boost target port
    Control: Mic Boost (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
Mixer: Plantronics Headset, USB Mixer [Port Headset [hw:1]]
  Source Port: Bass source port
    Control: Bass (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Source Port: Treble source port
    Control: Treble (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Source Port: Mic source port
    Control: Mic (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Select (boolean)
  Target Port: Bass target port
    Control: Bass (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Target Port: Treble target port
    Control: Treble (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Target Port: PCM target port
    Control: PCM (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
Mixer: USB Device 0x46d:0x8b2, USB Mixer [Port U0x46d0x8b2 [hw:2]]
  Source Port: Mic source port
    Control: Mic (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Select (boolean)

This might be either that the JVM does not support retrieving this information on OS X or your devices might not be supported. I would do two things:

  • try with different JVM
  • try on different OS

I run the code on linux and I got all the details correctly :

OS: Linux 2.6.38-12-generic/amd64
Java: 1.6.0_22 (Sun Microsystems Inc.)

Mixer: the ear-candy mixer [PulseAudio Mixer]
Mixer: Direct Audio Device: default, default, default [default [default]]
Mixer: Direct Audio Device: HDA Intel, ALC662 rev1 Analog, ALC662 rev1 Analog [Intel [plughw:0,0]]
Mixer: Direct Audio Device: Plantronics Headset, USB Audio, USB Audio [Headset [plughw:1,0]]
Mixer: Direct Audio Device: USB Device 0x46d:0x8b2, USB Audio, USB Audio [U0x46d0x8b2 [plughw:2,0]]
Mixer: HDA Intel, Realtek ALC662 rev1 [Port Intel [hw:0]]
  Source Port: Mic Boost source port
    Control: Mic Boost (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
  Source Port: Capture source port
    Control: Capture (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Select (boolean)
  Source Port: Capture source port
    Control: Capture (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Select (boolean)
  Target Port: Master target port
    Control: Master (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Headphone target port
    Control: Headphone (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Speaker target port
    Control: Speaker (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: PCM target port
    Control: PCM (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Line target port
    Control: Line (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Mic target port
    Control: Mic (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
  Target Port: Mic Boost target port
    Control: Mic Boost (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
Mixer: Plantronics Headset, USB Mixer [Port Headset [hw:1]]
  Source Port: Bass source port
    Control: Bass (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Source Port: Treble source port
    Control: Treble (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Source Port: Mic source port
    Control: Mic (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Select (boolean)
  Target Port: Bass target port
    Control: Bass (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Target Port: Treble target port
    Control: Treble (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
  Target Port: PCM target port
    Control: PCM (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Balance (float: from -1.0 to 1.0)
      Control: Mute (boolean)
Mixer: USB Device 0x46d:0x8b2, USB Mixer [Port U0x46d0x8b2 [hw:2]]
  Source Port: Mic source port
    Control: Mic (compound - values below)
      Control: Volume (float: from 0.0 to 1.0)
      Control: Select (boolean)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文