双向通信:
第一步:首先执行发送者类
第二步:这里执行 Tx 类
下载源:revoicechatusingjava.zip
警告:不要使用 sampe pc 执行每个文件使用不同的 pc 否则会出现错误(麦克风已经忙)
导入java.io.BufferedInputStream;
导入 java.io.BufferedOutputStream;
导入 java.io.ByteArrayOutputStream;
导入 java.io.IOException;
导入 java.net.ServerSocket;
导入 java.net.Socket;
导入 javax.sound.sampled.AudioFormat;
导入 javax.sound.sampled.AudioInputStream;
导入 javax.sound.sampled.AudioSystem;
导入 javax.sound.sampled.DataLine;
导入 javax.sound.sampled.LineUnavailableException;
导入 javax.sound.sampled.Mixer;
导入 javax.sound.sampled.SourceDataLine;
导入 javax.sound.sampled.TargetDataLine;
公共类发送者 {
ServerSocket MyService;
套接字客户端Socket = null;
BufferedInputStream 输入;
目标数据线目标数据线;
BufferedOutputStream 输出;
字节数组输出流字节数组输出流;
音频格式音频格式;
源数据线源数据线;
字节 tempBuffer[] = 新字节[10000];
sender() 抛出 LineUnavailableException{
尝试 {
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine)
AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
我的服务 = 新的 ServerSocket(500);
clientSocket = MyService.accept();
捕获音频();
输入 = 新的 BufferedInputStream(clientSocket.getInputStream());
out=new BufferedOutputStream(clientSocket.getOutputStream());
while(input.read(tempBuffer)!=-1){
sourceDataLine.write(tempBuffer,0,10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
私有音频格式 getAudioFormat(){
浮动 sampleRate = 8000.0F;
int sampleSizeInBits = 16;
整数通道 = 1;
布尔签名=真;
boolean bigEndian = false;
return new AudioFormat(
sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String s[]) throws LineUnavailableException{
sender s2=new sender();
}
私人无效 captureAudio() {
尝试 {
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("可用的混音器:");
播放线程=“新”字节数组输出流=“新”停止捕获=“假;”
cnt =" targetDataLine.read(tempBuffer,"> 0) { byteArrayOutputStream.write(tempBuffer, 0, cnt);
}
}
byteArrayOutputStream.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
私有 AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
整数通道 = 1;
布尔签名=真;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}
类 PlayThread 扩展线程 {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
while (in.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
导入 java.io.BufferedInputStream;
导入 java.io.BufferedOutputStream;
导入 java.io.ByteArrayOutputStream;
导入 java.io.IOException;
导入 java.net.Socket;
导入 javax.sound.sampled.AudioFormat;
导入 javax.sound.sampled.AudioInputStream;
导入 javax.sound.sampled.AudioSystem;
导入 javax.sound.sampled.DataLine;
导入 javax.sound.sampled.Mixer;
导入 javax.sound.sampled.SourceDataLine;
导入 javax.sound.sampled.TargetDataLine;
公共类 Tx {
boolean stopCapture = false;
字节数组输出流字节数组输出流;
音频格式音频格式;
目标数据线目标数据线;
音频输入流音频输入流;
BufferedOutputStream out = null;
BufferedInputStream in = null;
套接字 sock = null;
源数据线源数据线;
public static void main(String[] args) {
Tx tx = new Tx();
tx.captureAudio();
}
private void captureAudio() {
try {
sock = new Socket("192.168.1.51", 500);
out = new BufferedOutputStream(sock.getOutputStream());
in = new BufferedInputStream(sock.getInputStream());
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("可用的混音器:");
for (int cnt = 0; cnt < audioformat ="getAudioFormat();" datalineinfo ="new" 混音器 =" AudioSystem.getMixer(mixerInfo[3]);" targetdataline =" (TargetDataLine)" capturethread ="new" datalineinfo1 = “新”源数据线=(源数据线)“播放线程=新”字节数组输出流=新“停止捕获=假;”cnt=“targetDataLine.read(tempBuffer,">0){
byteArrayOutputStream.write(tempBuffer,0,cnt) ;
}
}
byteArrayOutputStream.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
浮动采样率 = 8000.0F;
int sampleSizeInBits = 16;
整数通道 = 1;
布尔签名=真;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}
类 PlayThread 扩展线程 {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
while (in.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习