【问题标题】:vocie chat using java and socket, delay when connect on LAN使用 java 和 socket 进行语音聊天,在 LAN 上连接时有延迟
【发布时间】:2016-03-06 15:44:22
【问题描述】:

我正在使用带有 java 的套接字编码语音聊天,两个客户端通话成功,但延迟 1-2 秒。我不知道为什么。导致网速或我的代码

线程播放器

public class player extends Thread{
public DataInputStream din = null;
public SourceDataLine audio_out;
byte byte_buff[] = new byte[256];
    @Override
    public void run(){
        try {
            while(din.read(byte_buff)!= -1){
                audio_out.write(byte_buff, 0, byte_buff.length);
            }
            audio_out.drain();
            audio_out.close();
        } catch (IOException ex) {
            Logger.getLogger(player.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                din.close();
            } catch (IOException ex) {
                Logger.getLogger(player.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

线程记录器

public class recorder extends Thread{
    public DataOutputStream dout = null;
    public TargetDataLine audio_in = null;
    byte byte_buff[] = new byte[256];
    @Override
    public void run(){
        try {
            while(true){
                int data = audio_in.read(byte_buff, 0, byte_buff.length);
                //audio_out.write(byte_buff, 0, byte_buff.length);
                System.out.println("sending");
                dout.write(byte_buff);
                dout.flush();
            }
        } catch (IOException ex) {
            Logger.getLogger(recorder.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                dout.close();
            } catch (IOException ex) {
                Logger.getLogger(recorder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

线程转移

public class transfer extends Thread{
    DataInputStream din;
    DataOutputStream dout;
    byte byte_buff[] = new byte[256];
    @Override
    public void run(){
        try {
            while(din.read(byte_buff)!= -1){
                dout.write(byte_buff, 0, byte_buff.length);
                System.out.println("tranferring");
            }
        } catch (IOException ex) {
            System.out.println("voice disconnect");
        } finally {
            try {
                din.close();
            } catch (IOException ex) {
                Logger.getLogger(transfer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

运行没问题,但延迟大约1-2s,两个客户端连接在局域网上。 请帮帮我

【问题讨论】:

  • 对于初学者来说,TCP 可能有很大的抖动,这是它从未用于语音聊天的主要原因之一。您应该改用 UDP。
  • 谢谢,我照你说的做了,非常感谢:))

标签: java sockets delay


【解决方案1】:

还有java的实时协议RTP,我相信是为了解决这类问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 2014-11-03
    • 1970-01-01
    • 2019-08-13
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多