语音通话的JAVA应用程序。 [英] JAVA app on voice call.
问题描述
我正在尝试开发Java语音呼叫应用程序。但我面临一个问题。每当我在两台不同的PC上的服务器端和客户端启动应用程序时,如果我是客户端,我在自己的PC上听到自己的声音,在服务器端无法听到任何声音,它完全没有功能。代码在我看来很好。有没有人可以帮我解决这个问题?
服务器端:
I am trying to develop a Java voice call application. but i am facing a problem. whenever i am starting the application on server side and client side on two different PCs, i hear my own voice in my own PC if i am a client and nothing is being able to be heard on server side, its totally nonfunctional. the code seems to me as fine. could anyone help me through this?
Server side :
import java.net.*;
import java.io.*;
import java.util.*;
public class Echo
{
public static void main(String[] args) throws Exception
{
ServerSocket serverSocket = new ServerSocket(3000);
while(true){Thread echoThread = new Thread(new EchoThread(serverSocket.accept()));
echoThread.start();}
}
}
class EchoThread implements Runnable
{
public static Collection<socket> sockets = new ArrayList<socket>();
Socket connection = null;
DataInputStream dataIn = null;
DataOutputStream dataOut = null;
public EchoThread(Socket conn) throws Exception
{
connection = conn;
dataIn = new DataInputStream(connection.getInputStream());
dataOut = new DataOutputStream(connection.getOutputStream());
sockets.add(connection);
}
public void run()
{
int bytesRead = 0;
byte[] inBytes = new byte[1];
while(bytesRead != -1)
{
try{bytesRead = dataIn.read(inBytes, 0, inBytes.length);}catch (IOException e){}
if(bytesRead >= 0)
{
sendToAll(inBytes, bytesRead);
}
}
sockets.remove(connection);
}
public static void sendToAll(byte[] byteArray, int q)
{
Iterator<socket> sockIt = sockets.iterator();
while(sockIt.hasNext())
{
Socket temp = sockIt.next();
DataOutputStream tempOut = null;
try
{
tempOut = new DataOutputStream(temp.getOutputStream());
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{tempOut.write(byteArray, 0, q);}catch (IOException e){}
}
}
}
客户方:
这里我有两节课。一个接收麦克风输入,将其发送到服务器,另一个从服务器获取数据,然后从扬声器中播放数据。
Client side:
here i have two classes. one takes microphone input, sends it to the server, and another takes data from the server, and plays that data out of a speaker.
import java.io.DataOutputStream;
import java.net.*;
import javax.sound.sampled.*;
public class Program
{
public final static String SERVER = JOptionPane.showInputDialog("Please enter server ip");
public static void main(String[] args) throws Exception
{
AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af);
TargetDataLine microphone = (TargetDataLine)AudioSystem.getLine(info);
microphone.open(af);
Socket conn = new Socket(SERVER,3000);
microphone.start();
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
int bytesRead = 0;
byte[] soundData = new byte[1];
Thread inThread = new Thread(new SoundReceiver(conn));
inThread.start();
while(bytesRead != -1)
{
bytesRead = microphone.read(soundData, 0, soundData.length);
if(bytesRead >= 0)
{
dos.write(soundData, 0, bytesRead);
}
}
System.out.println("IT IS DONE.");
}
}
import java.net.*;
import java.io.*;
import javax.sound.sampled.*;
public class SoundReceiver implements Runnable
{
Socket connection = null;
DataInputStream soundIn = null;
SourceDataLine inSpeaker = null;
public SoundReceiver(Socket conn) throws Exception
{
connection = conn;
soundIn = new DataInputStream(connection.getInputStream());
AudioFormat af = new AudioFormat(8000.0f,8,1,true,false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
inSpeaker.open(af);
}
public void run()
{
int bytesRead = 0;
byte[] inSound = new byte[1];
inSpeaker.start();
while(bytesRead != -1)
{
try{bytesRead = soundIn.read(inSound, 0, inSound.length);} catch (Exception e){}
if(bytesRead >= 0)
{
inSpeaker.write(inSound, 0, bytesRead);
}
}
}
}
推荐答案
您的代码正在运行 -
服务器拥有一组会话,当会话收到一条消息时,会将其转发给连接到所有会话的每个客户端。
我首先不会将数据发送回发件人。
然后,如果你需要在服务器上听到聊天,那么在那台机器上运行一个客户端。 />
为了防止将音频发送回发送它的原始客户端:
Your code is working -
Server holds a collection of sessions, when a session receives a message it relays it to each of the clients attached to all the sessions.
I would firstly not send the data back to the sender.
Then, if you need to hear the chat on the server then run a client on that machine.
To prevent sending the audio back to the original client that sent it:
Socket temp = sockIt.next();
// skip if this is the sender
if (temp == this.connection)
{
continue;
}
这篇关于语音通话的JAVA应用程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!