List of audio input and output devices in the Applet

I am launching a signed applet, which should provide the user with the ability to select input and output audio devices (similar to what Skype provides).

I borrowed the following code from another 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 expected to get a real list of my devices (My Settings panels show 3 output devices and 1 microphone). I am running Mac OS X 10.6.7.

Is there any other way to get this information from Java?

+4
source share
3 answers

For many years, there has been a very unfortunate limitation of the Java implementation for OS X being BTW, especially for this platform, that the "Java Sound Audio Engine" is the only software output audio line. As a result, everything that you send to this line, that is, outside of any java application that you create, will always be redirected to what was set as the default output in OS X, usually internal speakers. So JSAE is just Java terminology for "default audio output". To our understanding - unfortunately - this is still the case with the latest release.

Why miserable? Because it effectively disables even modest sound routing. Every day we work with these issues, and this requires any additional complexity. There is work around, but through third-party apps like SoundFlower and HiJack Pro. For example, www.soundPimp.com.

+3
source

Perhaps you can change and use this. The following code is used to select audio output devices on my two applets. I was only interested in the output lines, not the ports or input lines. The first part lists the options in the parameter group in the "Myrar" drop-down menu. The second part sets the mixer variable based on 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); } 

The mixer variable was selected here when selecting an option.

 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 is an abundance of console messages. I use this at http://hexara.com/VSL/JTheremin.htm

+1
source

It may be that the JVM does not support retrieving this information in OS X, or your devices may not be supported. I would do two things:

  • try using different jvm
  • try using a different OS

I run the code in linux and I correctly understood all the details:

 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
source

Source: https://habr.com/ru/post/1379198/


All Articles