Java实现录音功能
需要的jar包
链接: https://pan.baidu.com/s/1SxQ49HDl3JnuVhTV5G9EQA 提取码: zbb6
idea打入jar包的方法
import javax.sound.sampled.*;
public class Oatmeal {
AudioFormat format = new AudioFormat( 8000f, 16, 1, true, false);
TargetDataLine targetDataLine;
SourceDataLine sourceDataLine;
public void makeoatmead(){
try {
targetDataLine = AudioSystem.getTargetDataLine(format);
targetDataLine.open(format);
targetDataLine.start();
byte[] b = new byte[1024];
targetDataLine.read(b, 0, b.length);
this.opneoatmeal1(b);
} catch (Exception e) {
e.printStackTrace();
}
}
public void opneoatmeal1(byte[] b){
try {
sourceDataLine = AudioSystem.getSourceDataLine(format);
sourceDataLine.open(format);
sourceDataLine.start();
new Thread(new Runnable() {
@Override
public void run() {
//缓存音频数据
while(true) {
sourceDataLine.write(b, 0,targetDataLine.read(b, 0, b.length));//播放录制的声音
}
}
}).start();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Oatmeal().makeoatmead();
}
}