将实时音频流保存到java中的wave文件中

时间:2018-04-13 07:27:16

标签: java sockets

我在java服务器中使用DatagramSocket从Android客户端获取音频 现在我想保存已收到波形文件的音频。

java服务器代码是:

private static void receiveAudioFromAndroidClient() {
    try {
        DatagramSocket serverSocket = new DatagramSocket(50005);
        serverSocket1 = new ServerSocket(8072);


        byte[] receiveData = new byte[4096];
        // ( 1280 for 16 000Hz and 3584 for 44 100Hz (use AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat) to get the correct size)

        format = new AudioFormat(sampleRate, 16, 1, true, false);

        while (status == true) {
            DatagramPacket receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);

            serverSocket.receive(receivePacket);

            ByteArrayInputStream baiss = new ByteArrayInputStream(receivePacket.getData());

            ais = new AudioInputStream(baiss, format, receivePacket.getLength());


            // A thread solve the problem of chunky audio
            new Thread(new Runnable() {
                @Override
                public void run() {
                    toSpeaker(receivePacket.getData());
                }
            }).start();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

请帮帮我

1 个答案:

答案 0 :(得分:0)

使用AudioSystem将音频数据写入文件,如下所示:

AudioInputStream ais = new AudioInputStream(...);
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File("audio.wav"));