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。 –