我正在使用CMU狮身人面像库来录制声音。当我开始我的Java应用程序时,我仅按以下方式分配一次Recognizer和配置管理器:
cm = new ConfigurationManager(soundPart.class.getResource("hellongram.config.xml"));
recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.allocate();
而且,我的应用程序中有一个录音按钮。当用户单击它时,我使用以下代码记录声音:
Microphone microphone = (Microphone)MR.sp.cm.lookup("microphone");
if (!microphone.startRecording()) {
System.out.println("Cannot start microphone.");
MR.sp.recognizer.deallocate();
System.exit(1);
}
//MR.sp.pleaseStartSpeaking.setVisible(true);
while(true){
Result result = MR.sp.recognizer.recognize();
if(result!=null){
String resultText = result.getBestFinalResultNoFiller();
MR.sp.lblYouSearched.setVisible(true);
MR.sp.lblNewLabel.setVisible(true);
MR.sp.lblNewLabel.setText(resultText);
MR.textQuery = resultText.toLowerCase();
break;
}
}
这是我第一次这样做。但是,如果用户第二次单击录制按钮,则会引发错误“无法启动麦克风”。我在这里做错了。为什么我第二次不能获取麦克风
最佳答案
您可能想研究Microphone的RecordingThread here并重新阅读获得该代码here的页面,因为您使用的代码与您要执行的操作之间的区别在于您使用的代码不会启动和停止麦克风,它一直在记录。一个RecordingThread看起来就是您想要的东西,因为您可以轻松地调用start()和stop()来获取想要的东西。
关于java - 可以使用麦克风录制一次声音,但是不能第二次启动麦克风,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10014537/