java手机录音代码 安卓录音代码( 七 )


private void capture() {
try {
// 打开录音
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
// 创建独立线程进行录音
Thread captureThread = new Thread(new CaptureThread());
captureThread.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
// (2)播放ByteArrayOutputStream中的数据
private void play() {
try {
// 取得录音数据
byte audioData[] = byteArrayOutputStream.toByteArray();
// 转换成输入流
InputStream byteArrayInputStream = new ByteArrayInputStream(
audioData);
AudioFormat audioFormat = getAudioFormat();
audioInputStream = new AudioInputStream(byteArrayInputStream,
audioFormat, audioData.length / audioFormat.getFrameSize());
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
// 创建独立线程进行播放
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
// (3)停止录音
public void stop() {
stopCapture = true;
}
// (4)保存文件
public void save() {
// 取得录音输入流
AudioFormat audioFormat = getAudioFormat();
byte audioData[] = byteArrayOutputStream.toByteArray();
InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
audioInputStream = new AudioInputStream(byteArrayInputStream,
audioFormat, audioData.length / audioFormat.getFrameSize());
// 写入文件
try {
File file = new File("d:/myjava/test.wav");
AudioSystem
.write(audioInputStream, AudioFileFormat.Type.WAVE, file);
} catch (Exception e) {
e.printStackTrace();
}
}
// 取得AudioFormat
private AudioFormat getAudioFormat() {
float sampleRate = 16000.0F;
// 8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
// 8,16
int channels = 1;
// 1,2
boolean signed = true;
// true,false
boolean bigEndian = false;
// true,false
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
int cnt;
// 读取数据到缓存数据
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (cnt0) {
// 写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
class CaptureThread extends Thread {
// 临时数组
byte tempBuffer[] = new byte[10000];
public void run() {
byteArrayOutputStream = new ByteArrayOutputStream();
totaldatasize = 0;
stopCapture = false;
try {// 循环执行,直到按下停止录音按钮
while (!stopCapture) {
// 读取10000个数据
int cnt = targetDataLine.read(tempBuffer, 0,
tempBuffer.length);
if (cnt0) {
// 保存该数据
byteArrayOutputStream.write(tempBuffer, 0, cnt);
totaldatasize += cnt;
}
}
byteArrayOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}

推荐阅读