java 在java中使用套接字进行语音聊天或音频通话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31312621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
voice chat or audio call using socket in java
提问by Rafiq
Scenario 1:Voice Chat between two user/client working well, If and only If, both of them use headphone.
场景1:两个用户/客户端之间的语音聊天运行良好,当且仅当,他们都使用耳机。
Scenario 2:If any one of them don't use headphone, then voice chat is working but noise is increased gradually.
场景2:如果其中任何一个不使用耳机,则语音聊天正常,但噪音逐渐增加。
How it work
它是如何工作的
Client 1's microphone sound recorded and send to Client 2. But Client 1's speaker sound also recorded and send to Client 2. So, Client 2 hear his own sound and Client 1's sound repeatedly. And noise is increasing and increasing.
客户端 1 的麦克风声音录制并发送给客户端 2。但客户端 1 的扬声器声音也被录制并发送给客户端 2。因此,客户端 2 重复听到自己的声音和客户端 1 的声音。而且噪音越来越大。
Purpose
目的
I need to stop recording of speakers sound. How can I read/ record only microphone's input sound by filtering speaker's sound? Please help.
我需要停止录制扬声器声音。如何通过过滤扬声器的声音来仅读取/录制麦克风的输入声音?请帮忙。
Here is my code:
这是我的代码:
public class Server {
ServerSocket MyService;
Socket clientSocket = null;
InputStream input;
TargetDataLine targetDataLine;
OutputStream out;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
static Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
Server() throws LineUnavailableException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
try {
Mixer mixer_ = AudioSystem.getMixer(mixerInfo[0]);
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
MyService = new ServerSocket(500);
clientSocket = MyService.accept();
captureAudio();
input = new BufferedInputStream(clientSocket.getInputStream());
out = new BufferedOutputStream(clientSocket.getOutputStream());
while (input.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(
sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String s[]) throws LineUnavailableException {
Server s2 = new Server();
}
private void captureAudio() {
try {
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
Mixer mixer = null;
System.out.println("Available mixers:");
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
mixer = AudioSystem.getMixer(mixerInfo[3]);
if (mixer.isLineSupported(dataLineInfo)) {
System.out.println(mixerInfo[cnt].getName());
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
}
}
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread = new CaptureThread();
captureThread.start();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[10000];
@Override
public void run() {
try {
while (true) {
int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
out.write(tempBuffer);
out.flush();
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}}
And
和
public class Client {
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
BufferedOutputStream out = null;
BufferedInputStream in = null;
Socket sock = null;
SourceDataLine sourceDataLine;
public static void main(String[] args) {
Client tx = new Client();
tx.captureAudio();
}
private void captureAudio() {
try {
sock = new Socket("192.168.1.3", 500);
out = new BufferedOutputStream(sock.getOutputStream());
in = new BufferedInputStream(sock.getInputStream());
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("Available mixers:");
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
System.out.println(mixerInfo[cnt].getName());
}
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[2]);
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread = new CaptureThread();
captureThread.start();
DataLine.Info dataLineInfo1 = new DataLine.Info(
SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem
.getLine(dataLineInfo1);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
Thread playThread = new PlayThread();
playThread.start();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[10000];
@Override
public void run() {
byteArrayOutputStream = new ByteArrayOutputStream();
stopCapture = false;
try {
while (!stopCapture) {
int cnt = targetDataLine.read(tempBuffer, 0,
tempBuffer.length);
out.write(tempBuffer);
if (cnt > 0) {
byteArrayOutputStream.write(tempBuffer, 0, cnt);
}
}
byteArrayOutputStream.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 8;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];
@Override
public void run() {
try {
while (in.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
sourceDataLine.drain();
sourceDataLine.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
回答by Sebas
Typical "echo cancellation" issue when dealing with hands-free audio.
处理免提音频时典型的“回声消除”问题。
Try searching for a library who takes care of that because if you want to do it from scratch it is not trivial, matlab, adaptive filters, etc etc.
尝试寻找一个可以处理这个问题的库,因为如果你想从头开始,它不是微不足道的,matlab,自适应过滤器等。
Sebas
塞巴斯