27

I have been reading up on the Sound API for Java for a couple of days I am unable to make sense of it. I am decent programmer, I just having difficulty getting my head around the API.

I have been trying to capture audio from my microphone and display a wave graph in real time.

I am having trouble capturing audio, they say in the tutorial to do this, but I cant seem to get it to work.

Any suggestions and help would be much appreciated, a line by line answer would be ideal.

Please and thank you.

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class FindLine {

 public static void main (String[] args){

  AudioFormat format = new AudioFormat(22000,16,2,true,true);
  TargetDataLine line;
  DataLine.Info info = new DataLine.Info(TargetDataLine.class, 
      format); // format is an AudioFormat object
  if (!AudioSystem.isLineSupported(info)) {
      // Handle the error ... 
  }
  // Obtain and open the line.
  try {
      line = (TargetDataLine) AudioSystem.getLine(info);
      line.open(format);
  } catch (LineUnavailableException ex) {
      // Handle the error ... 
  }
 }

}
Mahdi Yusuf
  • 19,931
  • 26
  • 72
  • 101
  • 5
    I followed this tutorial. It's a great article about this theme: [http://www.developer.com/java/other/article.php/2105421/Java-Sound-Capturing-Microphone-Data-into-an-Audio-File.htm](http://www.developer.com/java/other/article.php/2105421/Java-Sound-Capturing-Microphone-Data-into-an-Audio-File.htm) – Ionică Bizău Nov 19 '12 at 16:21

1 Answers1

40

This will get you the default one set by your OS.

AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
TargetDataLine microphone = AudioSystem.getTargetDataLine(format);

To select a particular input device (TargetDataLine) it is better to enumerate the mixers and filter the name of the Mixer you want.

 Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
 for (Mixer.Info info: mixerInfos){
  Mixer m = AudioSystem.getMixer(info);
  Line.Info[] lineInfos = m.getSourceLineInfo();
  for (Line.Info lineInfo:lineInfos){
   System.out.println (info.getName()+"---"+lineInfo);
   Line line = m.getLine(lineInfo);
   System.out.println("\t-----"+line);
  }
  lineInfos = m.getTargetLineInfo();
  for (Line.Info lineInfo:lineInfos){
   System.out.println (m+"---"+lineInfo);
   Line line = m.getLine(lineInfo);
   System.out.println("\t-----"+line);

  }

 }
Saurabh
  • 71,488
  • 40
  • 181
  • 244
DannyM
  • 636
  • 7
  • 9
  • +1 I know this post is old, but I would like to know if the code you have provided above is all that it takes to read from a mic – samayo Jun 27 '13 at 13:43
  • 3
    Hi simon, This code only enumerates the available devices. After selecting the line you want (where reading is available). you can do as suggested in http://docs.oracle.com/javase/tutorial/sound/capturing.html – DannyM Sep 23 '13 at 12:26
  • How do you classify the elements of resulting list of "mixers" as input device / output device / transformer ? – Sam Ginrich Jan 17 '22 at 16:02