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

[6202 byte] By [Mordoxa] at [2007-10-2 0:36:05]
# 1

your asking the quest in the jmf forum and using some old type of handling the media

sun has given you very good frame work which reduce lot of coding and makes the life easy

then why are you going to old way.

first go and read jmf tutorial and implement your self dont go to get the code which some other people have written. do your self . you have energy to do. why are depending on others

read well tutorial and design. you will win i am sure

jmf makes life easy

konkodi221a at 2007-7-15 16:50:50 > top of Java-index,Security,Cryptography...
# 2
Hey Maddox,Im wondering if you have found an answer to your problem ?I am currently working on a project for my final year too and it involves taking songs off an audio cd im wondering do you know if Java supports this type of file format?or if you can give me any tips
Blogsa at 2007-7-15 16:50:50 > top of Java-index,Security,Cryptography...