Java语音聊天过滤掉自己的语音

时间:2014-04-07 20:16:15

标签: java sockets chat voice

我正在尝试使用声音API使用java实现语音聊天。它有一个服务器,它从所有客户端获取输入语音,并将它们中继回所有客户端。问题是说话的客户可以听他自己的声音。我发现了一个我发布的解决方案,但它无法正常工作。还有其他方法吗?

感谢您的帮助。

public class Server
{
 /*Server Code*/
    public static void main(String[] args) throws Exception
{
    ServerSocket serverSocket = new ServerSocket(7777);
    while(true){
        Thread echoThread = new Thread(
                new ServerThread(serverSocket.accept()));
        echoThread.start();
    }
}
}

class ServerThread implements Runnable
{
    public static Collection<Socket> sockets = new ArrayList<Socket>();
    public static int portId;
    Socket connection = null;
    DataInputStream dataIn = null;
    DataOutputStream dataOut = null;

    public ServerThread(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){
                e.printStackTrace();
            }
            if(bytesRead >= 0)
            {
                sendToAll(connection, inBytes, bytesRead);
            }
        }
        //sockets.remove(connection);
    }

    public static void sendToAll(Socket connection, byte[] byteArray, int q)
    {
        Iterator<Socket> sockIt = sockets.iterator();
        while(sockIt.hasNext())
        {
            Socket temp = sockIt.next();
            if(connection == temp){
                continue;
            }
            DataOutputStream tempOut = null;
            try{
                tempOut = new DataOutputStream(temp.getOutputStream());
            } 
            catch (IOException e1){
                e1.printStackTrace();
            }
            try{
                tempOut.write(byteArray, 0, q);
            }
            catch (IOException e){e.printStackTrace();}
        }
    }
}

public class Client {
/*Client Code*/
    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("localhost",7777);
        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.");
    }
}

public class SoundReceiver implements Runnable
{
  /*Helper Class*/
    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);
            }
        }
    }
}

0 个答案:

没有答案