62,614
社区成员
发帖
与我相关
我的任务
分享
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.io.BufferedInputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class MySound5_1 {
/**音频格式*/
private AudioFormat audioFormat = null;
/**麦克风 输入设备*/
private TargetDataLine targetDataLine = null;
/**输入设备*/
private SourceDataLine sourceDataLine = null;
/**输入混音器的信息*/
private DataLine.Info dataLine_info = null;
/**输出混音器的信息*/
private DataLine.Info out_dataLine_info = null;
public MySound5_1() throws LineUnavailableException, IOException{
audioFormat = new AudioFormat(8000.0f,8,1,true,false);//设置格式
dataLine_info = new DataLine.Info(TargetDataLine.class,audioFormat);//得到默认输入混音器的信息
targetDataLine = (TargetDataLine)AudioSystem.getLine(dataLine_info);//得到输入设备 麦克风
out_dataLine_info = new DataLine.Info(SourceDataLine.class,audioFormat);//
sourceDataLine = (SourceDataLine)AudioSystem.getLine(out_dataLine_info);//
byte[] b = new byte[100000];
targetDataLine.open(audioFormat);
targetDataLine.start();
sourceDataLine.open();
sourceDataLine.start();
int len = 0;
while((len = targetDataLine.read(b, 0, b.length)) > 0){//从输入设备读声音 麦克风
sourceDataLine.write(b, 0, len);//同时向输出设备送声音 喇叭
}
targetDataLine.close();
sourceDataLine.close();
System.exit(0);
}
public static void main(String[] args) {
try {
new MySound5_1();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}