Java Sound API – capturing microphone

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 ... 
  }
 }

}

How to capture sound from microphone with java sound API?

The tutorial http://download.oracle.com/javase/tutorial/sound/capturing.html does not cover how to select microphone. I am enumerating mixers with the following code System.out.println(Searching for

Input level monitoring with Java Sound API

I would like to monitor the microphone input level from Java Sound API (and create a small input level meter UI). I’m not interested in capturing only to see if the device is ready/working. Any idea i

Java Sound API: Capturing sound output from a Target Port

I’m writing a simple piece of software that streams audio over LAN. I have all of the network parts implemented, but what I’m stumbling on is using the Java Sound API. I have successfully captured aud

API for capturing sound on Windows

I need a C++ API to enumerate input devices and capture sound for Windows Vista, Windows 7 and Windows 8. If there is no common API I can use OS specific API for different versions of Windows. I found

actionscript sound capturing

I wrote something to capture sound from my microphone. I can visualise the sound data with SoundMixer.computeSpectrum. My problem: is there a way to mute the sound and still get sound data from SoundM

Graphic sound analyzer of microphone

I am building on an app which has a function which records the sound which comes in the microphone. It would be handy to give a graphical view of the incoming sound, like http://www.filebuzz.com/softw

Some Questions About the Java Sound API

I’m relatively new here and to the usage of Java’s Sound API and audio programming altogether. I have been wondering if it’s possible to do the following things with Java’s Sound API: extract the val

Java Sound API Initialization

I use the following code to play back sound in my game. import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.Audi

Realtime microphone sound level monitoring

I’m trying to access sound volume data from the microphone in realtime. I’ve tried AVAudioPlayer but it only monitors sounds from a source like an mp3 and not from a microphone. I’ve also tried The Sp

Java Sound refresh Lines list after attaching a microphone

I have a simple capture/playback Swing app that has to detect if there is no appropriate microphone attached to the computer and warn the user. After a lot of fiddling around I have found the only sol

Answers

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);

  }

 }