📄 audiorecorder.java
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class AudioRecorder implements Runnable {
private TargetDataLine line;
private Thread thread;
private String errStr = "";
private File file;
private String fileName;
private double duration, seconds;
private AudioInputStream audioInputStream;
//private FormatControls formatControls = new FormatControls();
public AudioRecorder(File file){
this.file = file;
}
public void start() {
errStr = null;
thread = new Thread(this);
thread.setName("Recorder");
thread.start();
}
public void stop() {
thread = null;
}
private void shutDown(String message) {
if ((errStr = message) != null && thread != null) {
thread = null;
// samplingGraph.stop();
// loadB.setEnabled(true);
// playB.setEnabled(true);
// pausB.setEnabled(false);
// auB.setEnabled(true);
// aiffB.setEnabled(true);
// waveB.setEnabled(true);
// captB.setText("Record");
System.err.println(errStr);
// samplingGraph.repaint();
}
}
public void run() {
duration = 0;
audioInputStream = null;
// define the required attributes for our line,
// and make sure a compatible line is supported.
AudioFormat format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, 44100, 16,
1, (16/8)*1, 44100, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
format);
if (!AudioSystem.isLineSupported(info)) {
shutDown("Line matching " + info + " not supported.");
return;
}
// get and open the target data line for capture.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
} catch (LineUnavailableException ex) {
shutDown("Unable to open the line: " + ex);
return;
} catch (SecurityException ex) {
shutDown(ex.toString());
return;
} catch (Exception ex) {
shutDown(ex.toString());
return;
}
// play back the captured audio data
ByteArrayOutputStream out = new ByteArrayOutputStream();
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead;
line.start();
while (thread != null) {
if((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
break;
}
out.write(data, 0, numBytesRead);
}
// we reached the end of the stream. stop and close the line.
line.stop();
line.close();
line = null;
// stop and close the output stream
try {
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
// load bytes into the audio input stream for playback
byte audioBytes[] = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
audioInputStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes);
long milliseconds = (long)((audioInputStream.getFrameLength() * 1000) / format.getFrameRate());
duration = milliseconds / 1000.0;
try {
audioInputStream.reset();
} catch (Exception ex) {
ex.printStackTrace();
return;
}
//samplingGraph.createWaveForm(audioBytes);
}
public void saveToFile(String name, AudioFileFormat.Type fileType) {
if (audioInputStream == null) {
reportStatus("No loaded audio to save");
return;
// } else if (file != null) {
// createAudioInputStream(file, false);
}
// reset to the beginnning of the captured data
try {
audioInputStream.reset();
} catch (Exception e) {
reportStatus("Unable to reset stream " + e);
return;
}
File file = new File(fileName = name);
try {
if (AudioSystem.write(audioInputStream, fileType, file) == -1) {
throw new IOException("Problems writing to file");
}
} catch (Exception ex) { reportStatus(ex.toString()); }
}
private void reportStatus(String msg) {
if ((errStr = msg) != null) {
}
System.out.println(errStr);
}
public void createAudioInputStream(File file, boolean updateComponents) {
if (file != null && file.isFile()) {
try {
this.file = file;
errStr = null;
audioInputStream = AudioSystem.getAudioInputStream(file);
fileName = file.getName();
long milliseconds = (long)((audioInputStream.getFrameLength() * 1000) / audioInputStream.getFormat().getFrameRate());
duration = milliseconds / 1000.0;
} catch (Exception ex) {
reportStatus(ex.toString());
}
} else {
reportStatus("Audio file required.");
}
}
public AudioInputStream getAudioInputStream() {
return audioInputStream;
}
public void setAudioInputStream(AudioInputStream audioInputStream) {
this.audioInputStream = audioInputStream;
}
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
public String getErrStr() {
return errStr;
}
public void setErrStr(String errStr) {
this.errStr = errStr;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public TargetDataLine getLine() {
return line;
}
public void setLine(TargetDataLine line) {
this.line = line;
}
public double getSeconds() {
return seconds;
}
public void setSeconds(double seconds) {
this.seconds = seconds;
}
public Thread getThread() {
return thread;
}
public void setThread(Thread thread) {
this.thread = thread;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -