一切正常,直到我尝试将我在PC上收到的内容放入wav
文件中。
我认为保存到wav文件部分存在问题。几乎尝试了所有事情,AudioSystem.write
等,似乎没有任何效果。
如果有人可以提供帮助,我会非常感激。这是代码:
class Server {
AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int sampleRate = 48000;
static DataLine.Info dataLineInfo;
static SourceDataLine sourceDataLine;
public static void main(String args[]) throws Exception {
OutputStream outstream = new FileOutputStream(new File("D:/output.wav"));
DatagramSocket serverSocket = new DatagramSocket(port);
int i = 0;
/**
* 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&&i<100) {
i++;
serverSocket.receive(receivePacket);
ais = new AudioInputStream(baiss, format, receivePacket.getLength());
try {
outstream.write(receivePacket.getData(), 0, receivePacket.getData().length);
} catch (Exception e) {
System.out.println("Not working in speakers...");
e.printStackTrace();
}
}
sourceDataLine.stop(); //after i has reacher 100 this happens
sourceDataLine.close();
outstream.close();
}
}