1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| import java.io.IOException;
import java.io.File;
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;
class Recorder implements Runnable {
private AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
private final int MONO = 1;
// private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 22050, 16, MONO, 2, 22050, true);
private AudioFormat format;
private TargetDataLine mic;
private Thread thread;
private File soundFile;
boolean pleaseWait = false;
public Recorder(File name,int qualite_) {
this.soundFile=name;
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, qualite_, 16, MONO, 2, qualite_, false);
}
public void startRecording() {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.err.println("Line not supported" + info);
System.exit(1);
}
try {
mic=(TargetDataLine)AudioSystem.getLine(info);
mic.open(format, mic.getBufferSize());
} catch (LineUnavailableException e) {
System.err.println("Line not available" + e);
System.exit(1);
}
thread = new Thread(this);
thread.start();
}
public void stopRecording() {
mic.stop();
mic.close();
}
public void run() {
mic.start();
AudioInputStream sound = new AudioInputStream(mic);
try {
AudioSystem.write(sound, fileType, soundFile);
System.out.println(thread.getState().toString());
} catch(IOException e) { System.out.println(e); }
}
} |
Partager