录音时出现问题
我有一个程序,首先播放:“请说出你的名字”。然后它会记录该名称。
我的部分代码是:
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(new File("sth.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
r.captureAudio();
这里 r 是主类的一个实例。
问题是录音是这样的:
它首先播放“请输入您的名字”,然后播放我用麦克风录制的内容。
我是一个初学者,对 Java 没有太多经验。
我应该进行哪些更改,以便录音仅包含麦克风输入而不是“请输入您的姓名”?
I have a program that first plays: "please say your name". It then records the name.
Part of my code is:
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(new File("sth.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
r.captureAudio();
Here r
is an instance of the main class.
The problem is that the recording goes like this:
It first plays "please enter your name" and then plays what I record with mic.
I am a beginner and don't have much experience with Java.
What changes should I make so that the recording contains only the microphone input and not "please enter your name"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我完全理解你的问题。
弹出
JOptionPane
(或模态JDialog
)将阻止 GUI,直到关闭为止。如果你弹出一个说“开始录制”,关闭后直接开始录制并弹出一个新的“停止录制”,第二个将停止其余的程序语句(但不会停止录制本身,只要它是在
线程
中完成的)。I am not sure I completely understand your question.
Popping a
JOptionPane
(or modalJDialog
) will block the GUI until dismissed.If you pop one to say 'start recording', start the recording directly after that is closed and pop a new one with 'stop recording', the second will stop the rest of the program statements (but not the recording itself, as long as it is done in a
Thread
).尝试调用
clip.drain()
,这将阻塞当前线程,直到剪辑播放完毕。之后就可以开始捕捉了。Try calling
clip.drain()
, this will block current thread until the clip has finished playing. After that you can start capturing.