Java 无法通过网络从麦克风传输音频字节

Java 无法通过网络从麦克风传输音频字节,java,udp,byte,buffer,audio-streaming,Java,Udp,Byte,Buffer,Audio Streaming,我尝试将麦克风中的数据记录为发送到java UDP服务器的字节数。但是服务器只播放白噪声。我想我对服务器没有问题。我很难将记录的字节传输到服务器 这是我的密码 public class Mic { public byte[] buffer; private int port; static AudioInputStream ais; public static void main(String[] args) { TargetDataLine line; DatagramPa

我尝试将麦克风中的数据记录为发送到java UDP服务器的字节数。但是服务器只播放白噪声。我想我对服务器没有问题。我很难将记录的字节传输到服务器

这是我的密码

public class Mic 
{
public byte[] buffer;
private int port;
static AudioInputStream ais;

public static void main(String[] args)
{
    TargetDataLine line;
    DatagramPacket dgp; 

    AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
    float rate = 44100.0f;
    int channels = 2;
    int sampleSize = 16;
    boolean bigEndian = true;
    InetAddress addr;


    AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);

    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    if (!AudioSystem.isLineSupported(info)) {
        System.out.println("Line matching " + info + " not supported.");
        return;
    }

    try
    {
        line = (TargetDataLine) AudioSystem.getLine(info);

        int buffsize = line.getBufferSize()/5;
        buffsize += 512; 

        line.open(format);

        line.start();   

        int numBytesRead;
        byte[] data = new byte[buffsize];

        addr = InetAddress.getByName("127.0.0.1");
        DatagramSocket socket = new DatagramSocket();
        while (true) {
               // Read the next chunk of data from the TargetDataLine.
               numBytesRead =  line.read(data, 0, data.length);
               // Save this chunk of data.
               dgp = new DatagramPacket (data,data.length,addr,50005);

               socket.send(dgp);
            }

    }catch (LineUnavailableException e) {
        e.printStackTrace();
    }catch (UnknownHostException e) {
        // TODO: handle exception
    } catch (SocketException e) {
        // TODO: handle exception
    } catch (IOException e2) {
        // TODO: handle exception
    }
}
}
还有我的服务器

public class Server 
{

AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int sampleRate = 44100;

static DataLine.Info dataLineInfo;
static SourceDataLine sourceDataLine;

public static void main(String args[]) throws Exception 
{
    System.out.println("Server started at port:"+port);

    DatagramSocket serverSocket = new DatagramSocket(port);

    /**
     * Formula for lag = (byte_size/sample_rate)*2
     * Byte size 9728 will produce ~ 0.45 seconds of lag. Voice slightly broken.
     * Byte size 1400 will produce ~ 0.06 seconds of lag. Voice extremely broken.
     * Byte size 4000 will produce ~ 0.18 seconds of lag. Voice slightly more broken then 9728.
     */

    byte[] receiveData = new byte[4096];

    format = new AudioFormat(sampleRate, 16, 1, true, false);
    dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
    sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
    sourceDataLine.open(format);
    sourceDataLine.start();

    //FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
    //volumeControl.setValue(1.00f);

    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

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

    while (status == true) 
    {
        serverSocket.receive(receivePacket);
        ais = new AudioInputStream(baiss, format, receivePacket.getLength());
        toSpeaker(receivePacket.getData());
    }

    sourceDataLine.drain();
    sourceDataLine.close();
}

public static void toSpeaker(byte soundbytes[]) {
    try 
    {
        System.out.println("At the speaker");
        sourceDataLine.write(soundbytes, 0, soundbytes.length);
    } catch (Exception e) {
        System.out.println("Not working in speakers...");
        e.printStackTrace();
    }
}
}

有人能帮我修一下并做一个正常的吗?

我想你没有把电话线。录音输入数据包。所以它只接收普通字节的数据