The
Targetdataline is the input (microphone) of the sound, while the sourcedataline is the output (stereo, headset).
for the entire procedure, see the following procedure. (after running, real-time recording from the microphone, real-time output).
Import java.io.*;
Import Javax.sound.sampled.AudioSystem;
Import Javax.sound.sampled.AudioInputStream;
Import Javax.sound.sampled.AudioFormat;
Import Javax.sound.sampled.SourceDataLine;
Import Javax.sound.sampled.DataLine;
Import Javax.sound.sampled.FloatControl;
Import Javax.sound.sampled.TargetDataLine;
public class Recordandplay {static volatile Boolean stop=false;
public static void Main (string[] args) {play (); //Play audio files public static void plays () {try {Audioformat Audioformat =//
New Audioformat (AudioFormat.Encoding.PCM_SIGNED, 44100F,//8, 1, 1, 44100F, false);
New Audioformat (AUDIOFORMAT.ENCODING.PCM_SIGNED,44100F, 2, 4, 44100F, true); Dataline.info Info = new Dataline.info (Targetdataline.class, Audioformat);
Targetdataline targetdataline = (targetdataline) audiosystem.getline (info);
Targetdataline.open (Audioformat); Final Sourcedataline Sourcedataline;
info = new Dataline.info (Sourcedataline.class, Audioformat);
Sourcedataline = (sourcedataline) audiosystem.getline (info);
Sourcedataline.open (Audioformat);
Targetdataline.start ();
Sourcedataline.start ();
Floatcontrol fc= (Floatcontrol) Sourcedataline.getcontrol (FloatControl.Type.MASTER_GAIN);
Double value=2;
float DB = (float) (Math.log (Value==0.0?0.0001:value)/math.log (10.0) *20.0);
Fc.setvalue (DB);
int nbyte = 0;
final int bufsize=4*100;
byte[] buffer = new Byte[bufsize];
while (Nbyte!=-1) {//system.in.read ();
Nbyte = targetdataline.read (buffer, 0, bufsize);
Sourcedataline.write (buffer, 0, nbyte);
} sourcedataline.stop ();
catch (Exception e) {e.printstacktrace ();
}
}
}