This article shows you how easy it is to capture sound coming into your computer’s microphone (or line in) and record the captured sound in to a WAV file, with help of Java Sound API. A small example program is provided to illustrate how to record sound for a specified duration. Let’s look at the Java Sound API first.

The package javax.sound.sampled.* is a part of Java Sound API which contains interfaces and classes that are dedicated for processing sampled audio by Java programming language.

Here are the typical steps to capture and record sound into a WAV file:

  •           Define an audio format of the sound source to be captured, using the class AudioFormat.
  •           Create a DataLine.Info object to hold information of a data line.
  •           Obtain a TargetDataLine object which represents an input data line from which audio data can be captured, using the method getLineInfo(DataLine.Info) of the AudioSystem class.
  •           Open and start the target data line to begin capturing audio data.
  •           Create an AudioInputStream object to read data from the target data line.
  •           Record the captured sound into a WAV file using the following method of the class AudioSystem:

write(AudioInputStream, AudioFileFormat.Type, File)


Note that this method blocks the current thread until the target data line is closed.

  •           Stop and close the target data line to end capturing and recording.

Here is the source code of a sample program which follows the steps above:

import javax.sound.sampled.*;

 * A sample program is to demonstrate how to record sound in Java
 * author:
public class JavaSoundRecorder {
	// record duration, in milliseconds
	static final long RECORD_TIME = 60000;	// 1 minute

	// path of the wav file
	File wavFile = new File("E:/Test/RecordAudio.wav");

	// format of audio file
	AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

	// the line from which audio data is captured
	TargetDataLine line;

	 * Defines an audio format
	AudioFormat getAudioFormat() {
		float sampleRate = 16000;
		int sampleSizeInBits = 8;
		int channels = 2;
		boolean signed = true;
		boolean bigEndian = true;
		AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
											 channels, signed, bigEndian);
		return format;

	 * Captures the sound and record into a WAV file
	void start() {
		try {
			AudioFormat format = getAudioFormat();
			DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

			// checks if system supports the data line
			if (!AudioSystem.isLineSupported(info)) {
				System.out.println("Line not supported");
			line = (TargetDataLine) AudioSystem.getLine(info);;
			line.start();	// start capturing

			System.out.println("Start capturing...");

			AudioInputStream ais = new AudioInputStream(line);

			System.out.println("Start recording...");

			// start recording
			AudioSystem.write(ais, fileType, wavFile);

		} catch (LineUnavailableException ex) {
		} catch (IOException ioe) {

	 * Closes the target data line to finish capturing and recording
	void finish() {

	 * Entry to run the program
	public static void main(String[] args) {
		final JavaSoundRecorder recorder = new JavaSoundRecorder();

		// creates a new thread that waits for a specified
		// of time before stopping
		Thread stopper = new Thread(new Runnable() {
			public void run() {
				try {
				} catch (InterruptedException ex) {


		// start recording



This console-based program will record sound from the microphone for 60 seconds then saves the recorded sound in to a file in .wav format under E:/Test/RecordAudio.wav (so make sure you created the parent directory first). You may want to save as .mp3 format, but unfortunately the Java Sound API only supports the .wav format.

You can change the record duration by modifying value of the constant RECORD_TIME at the beginning of the class.

Notice that there are two threads spawn in this program:

    • First thread (main thread): captures and records sound.
    • Second thread (the stopper thread): waits for a specified duration before closing the target data line. And because the main method is blocked by method call AudioSystem.write(), closing the target data line will continues the main method which exits the program.

Compile the program:


Run it:

java JavaSoundRecorder


Start capturing...

Start recording...


And check if the file RecordAudio.wav created under E:/Test directory and play it back.

The above example program is fixed for quick demo purpose. For a reusable sound recording component, see the article: A utility for recording sound in Java.


Recommended Book: Core Java, Volume II--Advanced Features (9th Edition) (Core Series)

Download this file ([Java Sound Recorder]2 kB
Thank you for giving this code it really helping me in my life.
apoplecticmaniac Sunday, 30 June 2013
Thanks a lot! You're a lifesaver ^_^
Which line of code makes sure that it has to capture from Mic ? What do I have to do to capture speaker-output ? What shall I do if I want to capture both MIC & Speaker-output simultaneously.
Hi Nitin,

As far as I know, the above code captures sound from the default microphone. And Java cannot capture sound of the speaker, because speaker is line-out, not line-in.
i tried using this code for recording audio but the problem is it make the file of 1 minute but when i open that file in vlc or window media player i can't hear anything which i have recorded.
hi sarthak,
If you don't hear the sound, the problem may be in the audio format was used to record, e.g. the sample rate, channels may not be supported by your computer. But as far as I know, almost computer supports the audio format specified in this tutorial.
Also, are you sure you microphone is working properly? It might be the cause as well.
Mangesh Kulkarni Monday, 31 March 2014
If I want to Captured the video of current working situation then what will i do for that[c[color=blue]olor=purple][/color] [/color]
Mangesh Kulkarni wrote:
If I want to Captured the video of current working situation then what will i do for that

In that case, you need need to capture the screen continuously. The basic technique is described here:
How to capture screenshot programmatically in Java
Start learning on Udemy today!