Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA中双向通信中的语音中断_Java_Lan_Voice_Livechat - Fatal编程技术网

JAVA中双向通信中的语音中断

JAVA中双向通信中的语音中断,java,lan,voice,livechat,Java,Lan,Voice,Livechat,我在JAVAFX中开发了一个桌面语音聊天应用程序。但在语音聊天中,语音会在连续的时间间隔内中断。任何人都可以帮我解决这个问题。代码如下所示。提前谢谢 我的服务器机器代码 public void startCalling() { inAudioThread = new Thread(new Runnable() { @Override public void run() {

我在JAVAFX中开发了一个桌面语音聊天应用程序。但在语音聊天中,语音会在连续的时间间隔内中断。任何人都可以帮我解决这个问题。代码如下所示。提前谢谢

我的服务器机器代码

public void startCalling()
    {
        inAudioThread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    InputStream in = socket.getInputStream();
                    byte[] buff = new byte[getBufferSize()];
                    Thread playSound = new Thread();
                    while((in.read(buff, 0, buff.length)) != -1)
                    {
                        playAudio(buff,playSound);
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        });

        outAudioThread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    serverSocket= new ServerSocket(9092);
                    serverSocket.setReuseAddress(true);
                    socket      = serverSocket.accept();

                    inAudioThread.start();

                    AudioFormat format = getAudioFormat();
                    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
                    tLine = (TargetDataLine)AudioSystem.getLine(info);
                    tLine.open(format);
                    tLine.start();

                    byte buffer[] = new byte[getBufferSize()];

                    out = new ByteArrayOutputStream();
                    bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());

                    running = true;
                    try
                    {
                        while(running)
                        {
                            int count = tLine.read(buffer, 0, buffer.length);
                            if (count > 0)
                            {
                                bufferedOutputStream.write(buffer, 0, count);
                                out.write(buffer, 0, count);
                            }
                        }
                        out.close();
                        bufferedOutputStream.close();
                    }
                    catch(IOException e)
                    {

                    }
                }
                catch(IOException | LineUnavailableException e)
                {

                }
            }
        });

        outAudioThread.start();
    }
服务器和客户端的通用功能

public AudioFormat getAudioFormat()
    {
        AudioFormat.Encoding encoding   = AudioFormat.Encoding.PCM_SIGNED;
        float   sampleRate              = 44100.0f;
        int     sampleSizeInBits        = 16;
        int     channels                = 2;
        int     frameSize               = (sampleSizeInBits / 8)* channels;
        boolean bigEndian               = true;

        return new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize,sampleRate,bigEndian);
    }
    public int getBufferSize()
    {
        return (int) getAudioFormat().getSampleRate()* getAudioFormat().getFrameSize();
    }
客户端代码与startCalling()函数相同,但是

serverSocket= new ServerSocket(9092);
serverSocket.setReuseAddress(true);
socket      = serverSocket.accept();

replaced with **socket = new socket("192.168.8.100",9092);**.

任何人都可以帮我解决语音干扰问题。再次提前感谢

您不保证每次都会填满缓冲区。在中的线程中,您错误地假设每次都将读取整个缓冲区

相反,您必须捕获读取的字节数,如下所示:

while( (bytesRead = in.read(buf)) != -1 ) {
    //play audio from buffer *bytesRead* bytes at a time
}
您可以使用which has方法读取缓冲区,直到缓冲区已满(请参阅readFullymethods)


还请注意,在OUT线程中,
OUT=newbytearrayoutputstream()while(){}
循环中写入它是不必要的。

不能保证每次都会填充缓冲区。在
中的线程中,您错误地假设每次都将读取整个缓冲区

相反,您必须捕获读取的字节数,如下所示:

while( (bytesRead = in.read(buf)) != -1 ) {
    //play audio from buffer *bytesRead* bytes at a time
}
您可以使用which has方法读取缓冲区,直到缓冲区已满(请参阅readFullymethods)


还请注意,在OUT线程中,
OUT=newbytearrayoutputstream()while(){}
循环中写入它是不必要的。

谢谢你,伙计…你的建议并不能解决我的问题。没问题,伙计。。。请比“它仍然不起作用”更精确。也许您的播放器有一些要求,数据应该如何输入(一次输入特定数量的数据)?你能分享你的
playAudio()
实现吗?谢谢你的建议并不能解决我的问题。没问题,伙计。。。请比“它仍然不起作用”更精确。也许您的播放器有一些要求,数据应该如何输入(一次输入特定数量的数据)?你能分享你的
playAudio()
实现吗?