Need Help with Sound Recording
Hi,
I'm a Comp. Sys. Eng. student in my final year and I've been trying to record sound with java for a couple of months now and its driving me stupid. I've looked around on just about every java source site I could find and have found many similar examples of sound recording, none seem to work on my machine. The problem is that I keep on getting either IOExceptions, or empty .wav files (as in the code below). The code included below is a shortened and modified version of some code I got from jsresources.org, the full link is http://www.jsresources.org/examples/AudioCommon.java.html.
I have not had any experience with multimedia in java before.
Can somebody please tell me what is wrong with this code? or is my java environment to blame?
Am I declaring my AudioFormat correctly?
Am I controlling my AudioInputStream correctly?
I'm using JDK1.4 in eclipse. Any help would be greatly appreciated.
/*
* AudioCommon.java
*
* This file is part of jsresources.org
*/
/*
* Copyright (c) 1999 - 2001 by Matthias Pfisterer
* All rights reserved.
*/
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class AudioCommon
{
private static booleanDEBUG = true;
public static void main(String Args[]){
AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
44100.0F, 16, 2, 4, 44100.0F, false);
TargetDataLine dataLine = getTargetDataLine("AK5370 ",
audioFormat,
5);
FileoutputFile = new File("C:\\testRecorder.wav");
try{
outputFile.createNewFile();
} catch(IOException ioe){
System.err.println("Couldn't create a new file: " + ioe);
}
AudioFileFormat.TypetargetType = AudioFileFormat.Type.WAVE;
SimpleAudioRecorderrecorder = new SimpleAudioRecorder(
dataLine,
targetType,
outputFile);
out("Press ENTER to start the recording.");
try
{
System.in.read();
}
catch (IOException e)
{
e.printStackTrace();
}
recorder.start();
out("Recording...");
long millis = System.currentTimeMillis();
while(System.currentTimeMillis() < millis + 5000);
recorder.stopRecording();
out("Recording stopped.");
if(outputFile.exists()){
System.out.println("File Exists.");
} else {
System.err.println("File does not Exist.");
}
System.exit(0);
}
public static TargetDataLine getTargetDataLine(String strMixerName,
AudioFormat audioFormat,
int nBufferSize)
{
/*
Asking for a line is a rather tricky thing.
We have to construct an Info object that specifies
the desired properties for the line.
First, we have to say which kind of line we want. The
possibilities are: SourceDataLine (for playback), Clip
(for repeated playback)and TargetDataLine (for
recording).
Here, we want to do normal capture, so we ask for
a TargetDataLine.
Then, we have to pass an AudioFormat object, so that
the Line knows which format the data passed to it
will have.
Furthermore, we can give Java Sound a hint about how
big the internal buffer for the line should be. This
isn't used here, signaling that we
don't care about the exact size. Java Sound will use
some default value for the buffer size.
*/
TargetDataLinetargetDataLine = null;
DataLine.Infoinfo = new DataLine.Info(TargetDataLine.class,
audioFormat, nBufferSize);
try
{
if (strMixerName != null)
{
Mixer.InfomixerInfo = getMixerInfo(strMixerName);
if (mixerInfo == null)
{
out("AudioCommon.getTargetDataLine(): mixer not found: " + strMixerName);
return null;
}
Mixermixer = AudioSystem.getMixer(mixerInfo);
targetDataLine = (TargetDataLine) mixer.getLine(info);
}
else
{
if (DEBUG) { out("AudioCommon.getTargetDataLine(): using default mixer"); }
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
}
/*
*The line is there, but it is not yet ready to
*receive audio data. We have to open the line.
*/
if (DEBUG) { out("AudioCommon.getTargetDataLine(): opening line..."); }
targetDataLine.open(audioFormat, nBufferSize);
if (DEBUG) { out("AudioCommon.getTargetDataLine(): opened line"); }
}
catch (LineUnavailableException e)
{
if (DEBUG) { e.printStackTrace(); }
}
catch (Exception e)
{
if (DEBUG) { e.printStackTrace(); }
}
if (DEBUG) { out("AudioCommon.getTargetDataLine(): returning line: " + targetDataLine); }
return targetDataLine;
}
}
>>>>>>>>>>>>>>>EOF<<<<<<<<<<<<<<<<
import java.io.File;
import javax.sound.sampled.*;
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;
}
public void stopRecording(){
m_line.stop();
m_line.flush();
m_line.close();
}
}
>>>>>>>>>>>>>>>EOF<<<<<<<<<<<<<<<<

