更新時間:2022-05-20 12:58:11 來源:動力節點 瀏覽2166次
雙向通信:
第一步:首先執行發送者類
第二步:這里執行 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();
}
}
}
}
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習