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

时间:2016-02-01 12:35:24

标签: java lan voice livechat

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

我的服务器机器代码

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();
    }

服务器和客户端的常用功能

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

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

客户端代码与startCalling()函数相同,但行

<html>

  <head>
    <title>Angular 2 QuickStart</title>

    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/app')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>

任何人都可以帮我解决语音中断问题。再次提前致谢

1 个答案:

答案 0 :(得分:0)

您无法保证每次都填充缓冲区。在 IN 线程中,您错误地认为每次读取整个缓冲区。

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

while( (bytesRead = in.read(buf)) != -1 ) {
    //play audio from buffer *bytesRead* bytes at a time
}

你可以使用IOUtils from Apache Commons方法读入缓冲区,直到缓冲区已满(参见 readFully 方法)。

另请注意,在OUT线程out = new ByteArrayOutputStream();中,然后在while(){}循环中写入它是不必要的。