Java 如何将声音打印为字节数组?还是整数数组?
我用java发出反相位声音。(反相位是反射波。x坐标不变,但y坐标颠倒。) 在反射声波之前,我必须从声音中获取字节数组(或int数组) 我从笔记本电脑的麦克风里听到声音 这是我的代码(我在web上获得了原始代码,它将声音记录为文件。我对其进行了少量修改) } 但是,Console在录制时不会打印任何内容。。。 这正好说明Java 如何将声音打印为字节数组?还是整数数组?,java,audio,audio-streaming,audio-recording,javasound,Java,Audio,Audio Streaming,Audio Recording,Javasound,我用java发出反相位声音。(反相位是反射波。x坐标不变,但y坐标颠倒。) 在反射声波之前,我必须从声音中获取字节数组(或int数组) 我从笔记本电脑的麦克风里听到声音 这是我的代码(我在web上获得了原始代码,它将声音记录为文件。我对其进行了少量修改) } 但是,Console在录制时不会打印任何内容。。。 这正好说明 com.sun.media.sound.DirectAudioDevice$DirectTDL@25154f_ 波动 按ENTER开始录制。 记录。。。 按ENTER键停止录制
com.sun.media.sound.DirectAudioDevice$DirectTDL@25154f_
波动
按ENTER开始录制。
记录。。。
按ENTER键停止录制。
录制已停止。
如果我编辑run()
像AudioSystem.write(流、文件类型、输出)代码>
而不是
int packet;
while((packet = audioInputStream.read()) != -1)
System.out.println(packet);
程序保存wav文件
我的程序出了什么问题?您没有像Uwe Allner所说的那样打印异常
我也试图纠正它,我认为结果一定是这样的:
public class NoiseController extends Thread {
private final TargetDataLine line;
private final AudioInputStream audioInputStream;
public NoiseController(final TargetDataLine line) {
this.line = line;
this.audioInputStream = new AudioInputStream(line);
}
@Override
public void start() {
line.start();
super.start();
}
public void stopRecording() {
line.stop();
line.close();
try {
audioInputStream.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
final int bufferSize = 1024;
int read = 0;
final byte[] frame = new byte[bufferSize];
while ((read = audioInputStream.read(frame)) != -1 && line.isOpen()) {
// only the first read bytes are valid
System.out.println(Arrays.toString(frame));
}
} catch (final IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(final String[] args) {
final AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
final DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
} catch (final LineUnavailableException lue) {
out("unable to get a recording line");
lue.printStackTrace();
System.exit(-1);
}
final AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
final NoiseController recorder = new NoiseController(targetDataLine);
System.out.println(targetDataLine);
System.out.println(targetType);
out("Press ENTER to start the recording.");
try {
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.start();
out("Recording...");
out("Press ENTER to stop the recording.");
try {
System.in.read();
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.stopRecording();
out("Recording stopped.");
}
private static void out(final String msg) {
System.out.println(msg);
}
}
也许您在catch(IOException ioe){
中禁止堆栈跟踪,您只是ioe.getStackTrace();
而不是ioe.printStackTrace();
。。。
public class NoiseController extends Thread {
private final TargetDataLine line;
private final AudioInputStream audioInputStream;
public NoiseController(final TargetDataLine line) {
this.line = line;
this.audioInputStream = new AudioInputStream(line);
}
@Override
public void start() {
line.start();
super.start();
}
public void stopRecording() {
line.stop();
line.close();
try {
audioInputStream.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
final int bufferSize = 1024;
int read = 0;
final byte[] frame = new byte[bufferSize];
while ((read = audioInputStream.read(frame)) != -1 && line.isOpen()) {
// only the first read bytes are valid
System.out.println(Arrays.toString(frame));
}
} catch (final IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(final String[] args) {
final AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
final DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
} catch (final LineUnavailableException lue) {
out("unable to get a recording line");
lue.printStackTrace();
System.exit(-1);
}
final AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
final NoiseController recorder = new NoiseController(targetDataLine);
System.out.println(targetDataLine);
System.out.println(targetType);
out("Press ENTER to start the recording.");
try {
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.start();
out("Recording...");
out("Press ENTER to stop the recording.");
try {
System.in.read();
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.stopRecording();
out("Recording stopped.");
}
private static void out(final String msg) {
System.out.println(msg);
}
}