大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java實現語音聊天的代碼

Java實現語音聊天的代碼

更新時間:2022-05-20 12:58:11 來源:動力節點 瀏覽2166次

使用 Java 進行語音聊天

雙向通信:

第一步:首先執行發送者類

第二步:這里執行 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();
}
}
}
}

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 99久久中文字幕 | 四月婷婷七月婷婷综合 | 狠狠色噜噜狠狠狠狠狠色综合久久 | 999国产高清在线精品 | 久久一二 | 伊人久久丁香色婷婷啪啪 | 免费看一级欧美毛片视频 | 精品久久九九 | 久久久福利 | 97久久影院| 久久伊人在 | 免费国产精品视频 | 国产精品欧美久久久久天天影视 | 福利在线网 | 久久久日韩精品国产成人 | 4虎最新地址 | 香蕉久人久人青草青草 | 91精品国产91久久久久青草 | 亚洲成人在线免费观看 | 成人性色生活片免费看爆迷你毛片 | 欧美成人性做爰 | 九九九九精品视频在线播放 | 色综合久 | 亚洲国产午夜精品理论片的软件 | 日韩专区中文字幕 | 国产一区在线观看视频 | 国内视频精品 | 色噜噜狠狠狠狠色综合久一 | 亚洲精品久久久久影 | 福利院肉动漫视频在线观看 | 久久国产免费福利资源网站 | 色酷综合 | 亚洲va欧美 | 亚洲精品一区二区三区五区 | 国产在线观看99 | 久草视频在线网 | 久久久久久综合一区中文字幕 | 毛片一区 | 国产香蕉一区二区精品视频 | 成人午夜影视全部免费看 | 欧美成人观看视频在线 |