Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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_Sockets_Audio_Tcp_Real Time - Fatal编程技术网

--Java--服务器无法正确播放来自客户端的音频--实时流--

--Java--服务器无法正确播放来自客户端的音频--实时流--,java,sockets,audio,tcp,real-time,Java,Sockets,Audio,Tcp,Real Time,我有一个客户端和服务器应用程序。 服务器请求音频流,客户端处理得很好 客户端继续设置AudioFormat(注意:两端的设置相同)和TargetDataLine。然后,它使用ByteArrayOutput流将数据写入套接字输出流 服务器接收数据并以线程方法读取。在每次缓冲区读取过程中,它会保存到一个AudioInputStream,然后将其传递到一个playSound方法,该方法经过线程化和同步,以继续播放声音 当我将playSound方法设为非线程时,它工作得很好,但有点小故障。 另外,我知道

我有一个客户端和服务器应用程序。 服务器请求音频流,客户端处理得很好

客户端继续设置AudioFormat(注意:两端的设置相同)和TargetDataLine。然后,它使用ByteArrayOutput流将数据写入套接字输出流

服务器接收数据并以线程方法读取。在每次缓冲区读取过程中,它会保存到一个AudioInputStream,然后将其传递到一个playSound方法,该方法经过线程化和同步,以继续播放声音

当我将playSound方法设为非线程时,它工作得很好,但有点小故障。 另外,我知道播放声音没有线程会导致声音帧阻塞 非常感谢您的任何帮助,我也欢迎您以任何方式使此音频流更加高效和快速

客户:

私有void captureAudio()引发CommandException{

    Socket session = new Socket(host_, port_); 
        try {
            final AudioFormat format = getFormat();
            DataLine.Info info = new DataLine.Info(
            TargetDataLine.class, format);
            final TargetDataLine line = (TargetDataLine)
            AudioSystem.getLine(info);
            line.open(format);
            line.start();

            int bufferSize = (int)format.getSampleRate() * format.getFrameSize();
            byte buffer[] = new byte[bufferSize];
            running = true;
                try {         
                    while (running) {

                            int count = line.read(buffer, 0, buffer.length);
                            if (count > 0) {
                               BufferedOutputStream out_ = null;
                               out_ = new BufferedOutputStream(socket_.getOutputStream());
                               out_.write(buffer, 0, count);
                               out_.flush();

                              }    
                    }
                    out_.close();
                    line.close();
                } catch (IOException e) {
                    throw new CommandException("I/O problems: " + e,Command.TRANSFER_ERROR);
                } 
        } catch (LineUnavailableException e) {
            throw new CommandException("Line unavailable: " + e,Command.ERROR);
        }
    else {
        throw new CommandException("Unable to Connect to Server",Command.CONNECTION_ERROR);
    }
}

private AudioFormat getFormat() {
    float sampleRate = 16000;
    int sampleSizeInBits = 16;
    int channels = 2;
    boolean signed = true;
    boolean bigEndian = true;
    return new AudioFormat(sampleRate,sampleSizeInBits, channels, signed, bigEndian);
}

服务器: 公共空读套接字(最终套接字){ 新线程(){

私人同步无效播放(最终音频输入流ais){ 新线程(){


}

您有什么解决方案吗?如果有,请与我们分享,谢谢
        @Override
        public void run() {
            InputStream input;
            try {
                input = socket.getInputStream();

                final AudioFormat format = getFormat();
                int bufferSize = (int)format.getSampleRate() * format.getFrameSize();
                byte buffer[] = new byte[bufferSize];
                int bytesRead;
                while (((bytesRead = input.read(buffer, 0, bufferSize)) != -1 ) ) {
                    if (bytesRead > 0) {
                        play(new AudioInputStream(input, format, buffer.length / format.getFrameSize()));
                    }
                }
                socket.close();
            } catch (Exception ex) {

            }


        }
    }.start();

}


private AudioFormat getFormat() {
  float sampleRate = 16000;
  int sampleSizeInBits = 16;
  int channels = 2;
  boolean signed = true;
  boolean bigEndian = true;
  return new AudioFormat(sampleRate, 
    sampleSizeInBits, channels, signed, bigEndian);
}
        @Override
        public void run() {
            try {
                final AudioFormat format = getFormat();
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
                SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);
                line.open(format);
                line.start();

                int bufferSize = (int) format.getSampleRate() 
                  * format.getFrameSize();
                byte buffer[] = new byte[bufferSize];

                int count;
                while ((count = ais.read(buffer, 0, buffer.length)) != -1) {
                  if (count > 0) {
                    line.write(buffer, 0, count);
                  }
                }
                line.drain();
                line.close();
                ais.close();
            } catch (LineUnavailableException ex) {
            } catch (IOException ex) {

            }
        }
    }.start();