File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes java sound I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java sound I/O" Watch "java sound I/O" New topic

java sound I/O

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I am trying Java Sound API to record some sound from microphone. I have found some souce code from
. But It doesn't work well. It can pass compiler, but the output wav file can not be played in windows media player.

Anyone has done this before? .




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

public class SimpleAudioRecorder
extends Thread
private TargetDataLinem_line;
private AudioFileFormat.Typem_targetType;
private AudioInputStreamm_audioInputStream;
private Filem_outputFile;

public SimpleAudioRecorder(TargetDataLine line,
AudioFileFormat.Type targetType,
File file)
m_line = line;
m_audioInputStream = new AudioInputStream(line);
m_targetType = targetType;
m_outputFile = file;

/** Starts the recording.
To accomplish this, (i) the line is started and (ii) the
thread is started.
public void start()
/* Starting the TargetDataLine. It tells the line that
we now want to read data from it. If this method
isn't called, we won't
be able to read data from the line at all.

/* Starting the thread. This call results in the
method 'run()' (see below) being called. There, the
data is actually read from the line.

/** Stops the recording.

Note that stopping the thread explicitely is not necessary. Once
no more data can be read from the TargetDataLine, no more data
be read from our AudioInputStream. And if there is no more
data from the AudioInputStream, the method 'AudioSystem.write()'
(called in 'run()' returns. Returning from 'AudioSystem.write()'
is followed by returning from 'run()', and thus, the thread
is terminated automatically.

It's not a good idea to call this method just 'stop()'
because stop() is a (deprecated) method of the class 'Thread'.
And we don't want to override this method.
public void stopRecording()

/** Main working method.
You may be surprised that here, just 'AudioSystem.write()' is
called. But internally, it works like this: AudioSystem.write()
contains a loop that is trying to read from the passed
AudioInputStream. Since we have a special AudioInputStream
that gets its data from a TargetDataLine, reading from the
AudioInputStream leads to reading from the TargetDataLine. The
data read this way is then written to the passed File. Before
writing of audio data starts, a header is written according
to the desired audio file type. Reading continues untill no
more data can be read from the AudioInputStream. In our case,
this happens if no more data can be read from the TargetDataLine.
This, in turn, happens if the TargetDataLine is stopped or closed
(which implies stopping). (Also see the comment above.) Then,
the file is closed and 'AudioSystem.write()' returns.
public void run()
catch (IOException e)

public static void main(String[] args)
if (args.length != 1 || args[0].equals("-h"))

/* We have made shure that there is only one command line
argument. This is taken as the filename of the soundfile
to store to.
StringstrFilename = args[0];
FileoutputFile = new File(strFilename);

/* For simplicity, the audio data format used for recording
is hardcoded here. We use PCM 44.1 kHz, 16 bit signed,
AudioFormataudioFormat = new AudioFormat(
44100.0F, 16, 2, 4, 44100.0F, false);

/* Now, we are trying to get a TargetDataLine. The
TargetDataLine is used later to read audio data from it.
If requesting the line was successful, we are opening
it (important!).
DataLine.Infoinfo = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLinetargetDataLine = null;
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);;
catch (LineUnavailableException e)
out("unable to get a recording line");

/* Again for simplicity, we've hardcoded the audio file
type, too.
AudioFileFormat.TypetargetType = AudioFileFormat.Type.WAVE;

/* Now, we are creating an SimpleAudioRecorder object. It
contains the logic of starting and stopping the
recording, reading audio data from the TargetDataLine
and writing the data to a file.
SimpleAudioRecorderrecorder = new SimpleAudioRecorder(

/* We are waiting for the user to press ENTER to
start the recording. (You might find it
inconvenient if recording starts immediately.)
out("Press ENTER to start the recording.");
catch (IOException e)
/* Here, the recording is actually started.

/* And now, we are waiting again for the user to press ENTER,
this time to signal that the recording should be stopped.
out("Press ENTER to stop the recording.");
catch (IOException e)

/* Here, the recording is actually stopped.
out("Recording stopped.");

private static void printUsageAndExit()
out("SimpleAudioRecorder: usage:");
out("\tjava SimpleAudioRecorder -h");
out("\tjava SimpleAudioRecorder <audiofile>");

private static void out(String strMessage)

/*** ***/

I agree. Here's the link:
subject: java sound I/O
It's not a secret anymore!