1
我正在用AWT框架创建一个基本的语音识别Java API。 我完成了我的编码,我的API也正在运行,但现在我想在识别正在进行时在我的API中引入“停止”按钮。 我正在尝试在语音识别开始后使此Stop按钮有效。 但我无法这样做。 我想打破停止按钮点击识别的while循环,而不是让while循环无限。 请帮我出您的建议.. 我的主类是:在java语音识别sphinx循环中启用按钮启用按钮API
package com.ongraph;
import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SpeechRecognizer extends JFrame {
/**
* Main class of the Application. Contains the UI of Application.
*/
private static final long serialVersionUID = 1L;
final JFrame frame = new JFrame("Speech To Text");
final JPanel panel = new JPanel();
final static JPanel panel_First = new JPanel();
final JPanel panel_Second = new JPanel();
final JPanel panel_Third = new JPanel();
final static TextArea textArea = new TextArea("", 10, 60);
final JButton speak = new JButton("Speak");
final static JButton clear = new JButton("Clear");
final static JButton stop = new JButton("Stop");
final BorderLayout borderLayout = new BorderLayout();
final static JLabel stop_Command = new JLabel(
"Speak 'Stop' to Stop Recording.");
final static SpeechToTextOperation speechToTextOperation = new SpeechToTextOperation();
public static void main(String args[]) {
new SpeechRecognizer();
}
public SpeechRecognizer() {
speak.setEnabled(true);
clear.setEnabled(true);
stop_Command.setVisible(false);
textArea.setVisible(true);
stop.setEnabled(false);
// When ever Speak button is pressed this method is invoked. voiceGet()
// method of SpeechToTextOperation class is called in this.
speak.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
speak.setEnabled(false);
try {
speechToTextOperation.voiceGet();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// When ever Clear button is pressed this methos is invoked. It clears
// the TextArea only after the Application has been stopped.
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
speechToTextOperation.voiceStop();
textArea.setText("");
speak.setEnabled(true);
}
});
frame.add(panel_First, BorderLayout.NORTH);
frame.add(panel_Second, BorderLayout.CENTER);
frame.add(panel_Third, BorderLayout.SOUTH);
panel_First.add(speak);
panel_First.add(stop);
panel_Second.add(clear);
panel_Third.setLayout(borderLayout);
panel_Third.add(stop_Command, BorderLayout.CENTER);
panel_Third.add(textArea, BorderLayout.SOUTH);
frame.setSize(600, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
有狮身人面像代码的类是:
package com.ongraph;
import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
public class SpeechToTextOperation {
ConfigurationManager cm;
SpeechRecognizer speechRecognizer;
Result result;
Recognizer recognizer;
Microphone microphone;
private final static String STOP = "stop";
private final static String XML_FILE = "helloworld.config.xml";
/*Called by the click on Speak button. It recognizes your voice, matches it
with Grammar file and displays the words spoken.*/
public void voiceGet() throws InterruptedException {
String resultString = null;
int count_Check = 0;
if (cm == null) {
cm = new ConfigurationManager(getClass().getClassLoader()
.getResource(XML_FILE));
}
if (recognizer == null) {
recognizer = (Recognizer) cm.lookup("recognizer");
microphone = (Microphone) cm.lookup("microphone");
microphone.clear();
}
recognizer.allocate();
if (!(microphone.startRecording())) {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}
instructions();
while (true) {
System.out
.println("Start speaking. Speak 'Stop' to Stop Recording.");
if (count_Check == 0) {
SpeechRecognizer.textArea.append("\n Start speaking...\n");
count_Check++;
}
SpeechRecognizer.stop.setEnabled(true);
Result result = recognizer.recognize();
resultString = result.getBestFinalResultNoFiller();
if (!resultString.contains(STOP)) {
SpeechRecognizer.textArea.append(resultString + "\n");
} else {
SpeechRecognizer.textArea
.append("'Application Stopped. Press 'Speak' again to restart'");
recognizer.deallocate();
microphone.stopRecording();
break;
}
}
}
/*Clears the Microphone so that new words can be recognized and frees the
ConfigurationManager Object.*/
public void voiceStop() {
microphone.clear();
cm = null;
}
// Provides you instruction how to stop the Application
public void instructions() {
// TODO Auto-generated method stub
SpeechRecognizer.stop_Command.setVisible(true);
}
}
感谢您的回复。 但实际上问题是,只要我点击“说话”按钮,我的API UI就会被挂起。我无法按下任何按钮,任何按钮都不会响应,除非我从此while循环出来。 – user3703157
如果您的UI在说话过程中停止,您可能希望让它在单独的线程上运行并异步运行。查看使用异步任务的http://developer.android.com/reference/android/os/AsyncTask.html。 –