起航学习网

- 让每个人都能学到最前沿新知识、新技能!
起航学习网
当前位置: 起航学习网 > 短期培训 > 编程语言 > Java实现语音聊天的代码

Java实现语音聊天的代码

时间:2022-06-08 14:17:21来源:IT培训网 作者:Java学习网 已有: 名学员访问该课程

  快捷搜索:java实现语音聊天

前言: 使用 Java 进行语音聊天 双向通信: 第一步:首先执行发送者类 第二步:这里执行 Tx 类 下载源:revoicechatusingjava.z

使用 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();
}
}
}
}

 

文章出自:http://qh.itpxw.cn/peixun/software/2022121708.html

文章标题:Java实现语音聊天的代码



免责声明:本站文章均由入驻起航学习网的会员所发或者网络转载,所述观点仅代表作者本人,不代表起航学习网立场。如有侵权或者其他问题,请联系举报,必删。侵权投诉

你也许会喜欢如下的文章?
(责任编辑:深圳学历教育网)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
培训学校
IT培训网 访问该机构站点 报名留言 加为好友 用户等级:注册会员 用户级别:10 机构名称:IT培训网 联 系 人:罗老师 联系电话:13783581536 联系手机:13783581536 在线客服:起航学习网客服 在 线 QQ:起航学习网客服 电子邮件: 网站域名:http://www.itpxw.cn 注册时间:2016-07-18 11:07 最后登录:2024-02-20 13:02
推荐内容