I am making a voice chat/messenger program and I got voice to work with one person in the chat but when i add a second to it the voices are laggy and cut up. I think the problem is in the Client Audio Receive class. If you don't think it is I will link the rest in a pastebin.
package client;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class ClientAudioRec implements Runnable {
private ObjectInputStream i2;
private Socket s;
private AudioFormat af;
public ClientAudioRec(Socket s2, AudioFormat audioformat) {
s = s2;
af = audioformat;
}
public void run() {
try {
i2 = new ObjectInputStream(s.getInputStream());
} catch (IOException e2) {
e2.printStackTrace();
}
SourceDataLine inSpeaker = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
try {
inSpeaker = (SourceDataLine)AudioSystem.getLine(info);
inSpeaker.open(af);
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
int bytesRead = 0;
byte[] inSound = new byte[100];
inSpeaker.start();
while(true)
{
try{
bytesRead = i2.read(inSound, 0, inSound.length);
} catch (Exception e){
e.printStackTrace();
}
if(bytesRead >= 0)
{
inSpeaker.write(inSound, 0, bytesRead);
}
}
}
}