通过TCP套接字Java读/写流式音频

时间:2015-08-12 05:11:55

标签: java sockets tcp audio-streaming

我是Java和套接字编程的新手,我正在寻找更多关于正确方向/验证思路的起点/点。

总的来说,我想要实现的想法是:

  1. 捕获我录制为.wav
  2. 的音频
  3. 通过TCP将其发送到我的服务器
  4. 决定是否要播放录制的音频(在我的tcp服务器中处理和播放)或通过TCP发送到其他服务器进行处理(如转录器或类似内容)。
  5. 我遇到的麻烦是服务器端,而且这是因为我不完全理解套接字编程,说实话。我希望将音频作为原始音频接收到我的服务器(将字节/二进制数组作为参数传递)。对于大多数文档,我发现他们基本上都说开放套接字,打开输入/输出流,读/写,关闭流,关闭套接字。这适用于普通文本而非音频。我确信这可以用音频来完成。对于音频.wav,一般的想法是什么?是否有我不知道的API涵盖了这个?




    更新:2015年8月30日
    这是我到目前为止的TCP客户端/服务器代码。现在,"捕获的音频"我正在使用的只是我读入客户端输出流的现有.wav文件。我现在面临的问题是创建的.wav听起来并不像原版。这听起来像是噪音,而且时间要短得多。

    TCP客户端代码:

    Socket serverSocket = null;
    DataOutputStream out = null;
    BufferedReader in = null;
    
    try {
        serverSocket = new Socket(serverHostname, port);
        out = new DataOutputStream(serverSocket.getOutputStream());
        in = new BufferedReader(
                  new InputStreamReader(serverSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + serverHostname);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: " + serverHostname);
        System.exit(1);
    }
    
    DataInputStream stdIn = new DataInputStream( 
                                  new FileInputStream(".wav location"));
    
    int readBytes;
    byte[] temp = new byte[1024];
    
    while( (readBytes = stdIn.read(temp, 0, temp.length)) != -1){
        out.write(temp, 0, readBytes);
    }
    
    out.flush();
    out.close();
    in.close();
    stdIn.close();
    serverSocket.close();
    }
    

    到目前为止,这是我的服务器代码:

    public void clientConnect(int socketPort) {
            ServerSocket s_Socket = null; 
    
            try {
                 s_Socket = new ServerSocket(socketPort);
    
                 System.out.println ("SERVER Connection Socket Created");
                 try {
    
                     System.out.println ("Waiting for CLIENT Connection");
                      while (true){
                          new TcpAudioStreaming (c_Socket = s_Socket.accept());
                          System.out.println("CLIENT: " + c_Socket.getInetAddress());
                          System.out.println("PORT: " + c_Socket.getPort());
                          System.out.println("LOCAL PORT: " + c_Socket.getLocalPort());
                      }
                 }     
                 catch (IOException e) { 
                      System.err.println("Accept failed."); 
                      System.exit(1); 
                 }
            }
    
            catch (IOException e){
                 System.err.println("Could not listen on port: " + socketPort); 
                 System.exit(1); 
            }      
            finally {
    
                 try {
                     s_Socket.close(); 
                 }
                 catch (IOException e){
                      System.err.println("Could not close port: " + socketPort); 
                      System.exit(1); 
                 }
            }
        }
    
    
        public void run() {
    
            boolean end = false;
            System.out.println ("New Communication Thread Started");
    
            try {
                //DataOutputStream outStream = new DataOutputStream(c_Socket.getOutputStream()); 
                ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
                DataInputStream inStream = new DataInputStream(c_Socket.getInputStream()); 
    
                int read;
                byte[] temp = new byte[1024];
    
                while( (read = inStream.read(temp, 0, temp.length)) != -1){
                    outStream.write(temp, 0, read);
                }
    
                outStream.flush();
                byte[] audioBytes = outStream.toByteArray();
    
                writeWave(audioBytes);
    
                outStream.close(); 
                inStream.close(); 
                c_Socket.close();
    
    
            } 
            catch (IOException e) {
                  System.err.println("Problem with Communication Server");
                  System.exit(1); 
            } 
    
        }
    
        public void writeWave(byte[] audioArry) {
    
            String filePath = "new .wav path";
    
            AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, false);
    
            try {
    
                ByteArrayInputStream inStream = new ByteArrayInputStream(audioArry);
                long length = (long)(audioArry.length / audioFormat.getFrameSize());
                AudioInputStream audioInputStreamTemp = new AudioInputStream(inStream, audioFormat, length);
    
    
                File fileOut = new File(filePath);
    
                if (AudioSystem.isFileTypeSupported(AudioFileFormat.Type.WAVE, audioInputStreamTemp)) {
                    System.out.println("Trying to write");
                    AudioSystem.write(audioInputStreamTemp, AudioFileFormat.Type.WAVE, fileOut);
                    System.out.println("Written successfully");
                }       
            }
            catch(Exception e) {
                e.printStackTrace();
            }
    
        }
    

1 个答案:

答案 0 :(得分:1)

Java套接字或TCP不区分ASCII和二进制数据。是应用程序进行解释。

从一个简单的客户端/服务器应用程序开始,然后从那里继续前进。对于介绍,您可以看到https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html