Using Java sound API to record to a wav file

N

Nomak

Hello,

i can't get how Java sound was done. I would like to do that:
- enable the user to choose from a combo box an input device (webcam
microphone, line-in, etc)
- enable the user to choose from a combo box an audio format
_compatible_ with the selected input device (which means the combo is
updated after the input device is selected)

Can somebody help me?
 
P

Phil Staite

Look into javax.sound.sampled.*

You'll want to check out the AudioFormat class. You fill one in,
something like:

// set up for simple low-rate mono recording
float rate = 11025;
int samplesSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
AudioFormat af = new AudioFormat( rate,
sampleSizeInBits,
channels,
signed,
bigEndian );



DataLine.Info dli = new DataLine.Info( TargetDataLine.class, af );
TargetDataLine recLine = (TargetDataLine) AudioSystem.getLine( dli );
recLine.open( af );
recLine.start();
// then start reading in buffers
byte[] buffer = new byte[11025 * 10]; // say 10 sec worth of audio data
int count = recLine.read( buffer, 0, buffer.length );
if( count > 0 )
// process audio data
else
// WTF happened?



If you think this looks ugly, believe me, working with the java audio
interface is a lot easier than working with say the raw Windows audio
interface... It is both fragile and unforgiving...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top