2011-11-01 145 views
4

我正在運行一個簽名的applet,它需要爲用戶提供選擇輸入和輸出音頻設備的能力(類似於Skype提供的)。在Applet中列出輸入和輸出音頻設備

我借用其他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";} 
} 

但我得到什麼:

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獲取該信息?

回答

3

多年來,它一直是OS X的Java實現的一個非常不幸的限制,BTW特別針對該平臺,「Java聲音音頻引擎」是唯一可編程的輸出音頻li東北。因此,無論您發送到此行,即從您製作的任何Java應用程序中發出的任何內容,都將始終路由到OS X中默認輸出的設置,通常是內置揚聲器。所以JSAE只是用於「默認音頻輸出」的Java術語。根據我們的理解 - 可悲的是 - 最新版本仍然如此。

爲什麼不幸?因爲它甚至可以有效地禁用謙遜的音頻路由。我們每天都在處理這些問題,並要求增加各種複雜性。有解決方法,但通過第三方應用程序SoundFlower和HiJack Pro。例如www.soundPimp.com。

1

也許你可以修改和使用它。以下代碼用於在我的兩個Applets上選擇音頻輸出設備。我只對輸出線感興趣,而不是端口或輸入線。第一部分列出Menubar下拉菜單中選項組中的選項。第二部分根據選定的選項設置混合器變量。

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

+0

菲爾,謝謝你,但是你的代碼和我的代碼一樣(就列出的設備而言)。我的Mac上有3個輸出設備,但java聲音API只列出了:「Java Sound Audio Engine」。這也是您的小應用程序首選項中的唯一選項。 –

+0

@Jhonny - 抱歉聽到這沒有更多的幫助。您是否看到了Option選項中的選項和被「拒絕」並在控制檯上列出的選項?還沒找到東西?拖動。 –

+0

是的,我得到了:混頻器拒絕,非法參數:麥克風(粉紅色後方),版本未知版本。但沒有列出的輸出設備。 –

1

這可能是要麼在JVM不支持獲取在OS X或您的設備,這個信息可能不支持使用此功能。我會做兩件事情:

  • 不同的JVM嘗試不同的OS
  • 嘗試

我在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) 
+1

是的,這個問題只發生在Mac Os上。基本上,如果有一個適用於Mac和Win的api/native API。 –

+0

您使用的是最新的Oracle JVM嗎?如果這不起作用,那麼它一定是甲骨文無法實​​施它的嚴重原因。無論如何有一個desciption如何使用本機代碼訪問mac上的硬件:http://developer.apple.com/library/mac/#documentation/DeviceDrivers/Conceptual/AccessingHardware/AH_Intro/AH_Intro.html –

+1

我使用默認隨Mac OS X一起提供。我不能要求用戶升級他們的JVM。 –