Java语音聊天错误

时间:2016-04-28 21:16:44

标签: java objectinputstream voice-recording javax.sound.sampled

我正在制作语音聊天程序。我有两台服务器用于语音,一台用于消息。当我连接两个人时,我收到此错误,提前感谢您。我附加了客户端代码,ClientAudio代码和客户端接收代码

java.io.StreamCorruptedException: invalid type code: 00
at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2508)
at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2543)
at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2702)
at java.io.ObjectInputStream.read(ObjectInputStream.java:865)
at client.chat$ClientAudioRec.run(chat.java:388)
at java.lang.Thread.run(Thread.java:745)

它在

上调用错误
try {
    bytesRead = ((ObjectInput) i2).read(inSound, 0, inSound.length);
} catch (Exception e) {
    e.printStackTrace();
}

代码

    public class Client implements Runnable {                                                   // CLIENT
    private String msg;
    public void run() {
        try {
            s1 = new Socket(ipAddress, port);
            s2 = new Socket(ipAddress, 1210);
            o1 = new ObjectOutputStream(s1.getOutputStream());
            o1.writeObject(name);
            serverListModel.addElement(name);
            i1 = new ObjectInputStream(s1.getInputStream());
            Thread voice = new Thread(new ClientAudio());
            voice.start();
            while(true) {
                msg = (String) i1.readObject();
                    String[] namePart = msg.split("-");
                    if(namePart[0].equals("AddName") && !namePart[1].equals(name) && !serverListModel.contains(namePart[1])) {
                        serverListModel.addElement(namePart[1]);
                    }
                    if(namePart[0].equals("RemoveName") && !namePart[1].equals(name)) {
                        serverListModel.removeElement(namePart[1]);
                    }
                    if(!msg.equals(null) && !namePart[0].equals("AddName") && !namePart[0].equals("RemoveName")) {
                        chatWindow.append(msg+"\n");
                    }
                }
        } catch (IOException | ClassNotFoundException e) {
            chatWindow.append("Server Closed");
            e.printStackTrace();
            try {
                s1.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            mainWindow(true);
        }
    }
}

public class ClientAudio implements Runnable {                                                  // CLIENT AUDIO
    public void run() {
        try {
            o2 = new ObjectOutputStream(s2.getOutputStream());
            System.out.println("AUDIO");
            int bytesRead = 0;
            byte[] soundData = new byte[1];
            Thread car = new Thread(new ClientAudioRec());
            car.start();

            while(true) {
                bytesRead = mic.read(soundData, 0, bytesRead);
                if(bytesRead >= 0) {
                    o2.write(soundData, 0, bytesRead);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class ClientAudioRec implements Runnable {                                                       // CLIENT AUDIO REC

    public void run() {
        i2 = new ObjectInputStream(s2.getInputStream());
        System.out.println("REC");
        SourceDataLine inSpeaker = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        try {
            inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
            inSpeaker.open(af);
        } catch (LineUnavailableException e1) {
            System.out.println("ERROR 22");
            e1.printStackTrace();
        }

        int bytesRead = 0;
        byte[] inSound = new byte[100];
        inSpeaker.start();
        while(bytesRead != -1)
        {
            try{
                bytesRead = ((ObjectInput) i2).read(inSound, 0, inSound.length);
                } catch (Exception e){
                e.printStackTrace();
            }
            if(bytesRead >= 0)
            {
                inSpeaker.write(inSound, 0, bytesRead);
            }
        }
    }
}

0 个答案:

没有答案