-2

我正在尝试将语音转换为文本并将其显示在 AWT textArea 上。但是语音到文本转换器功能的输出是在一个while循环内生成的,我无法在textArea上显示它。我得到一个空指针异常;请有人帮忙。

public class Speechrec  {

    private static TextArea textArea;
    String resultText;
    private String dr;

    public void recognizer(String[] args) {

        try {
            URL url;
            if (args.length > 0) {
                url = new File(args[0]).toURI().toURL();
            } 
            else {
                url = Speechrec.class.getResource("speechrec.config.xml");
            }    
            System.out.println("Loading...");    
            ConfigurationManager cm = new ConfigurationManager(url);    
            Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
            Microphone microphone = (Microphone) cm.lookup("microphone");    
            /* allocate the resource necessary for the recognizer */
            recognizer.allocate();    
            /* the microphone will keep recording until the program exits */
            if (microphone.startRecording()) {    
            System.out.println("Say: some greetings");
            while (true) {
                System.out.println("Start speaking. Press Ctrl-C to quit.\n");

                Result result = recognizer.recognize();

                if (result != null) {
                    String resultText = result.getBestFinalResultNoFiller();            
                    textArea.setText(resultText);
                } 
                else {
                    System.out.println("I can't hear what you said.\n");
                }
            }
            } 
            else {
               System.out.println("Cannot start microphone.");
               recognizer.deallocate();
               System.exit(1);
            }
        } 
        catch (Exception) {                
            // exception handling
        }
    }        

public static void main( final String[] args) throws IOException {
    Speechrec sp1=new Speechrec();      
    Frame frame=new Frame("speech to sign language converter");
    TextArea textarea=new TextArea (05,30);     
    Button button = new Button("Start speaking"); 
    // ...        
    frame.add(button,BorderLayout.SOUTH); 
    // ...
    frame.setLayout(new FlowLayout(FlowLayout.TRAILING,50,15));
    frame.setSize(500,400); 
    frame.setVisible(true);

    button.addActionListener(new ActionListener() {           
         public void actionPerformed(ActionEvent e1) {      
             Speechrec sp=new Speechrec();
         sp.recognizer(args);
          }});
    }    
}
4

2 回答 2

1

将您的文本区域声明为类中的私有静态字段。然后您可以轻松访问它textArea.setText("something");

private static TextArea textArea;

public static void main( final String[] args) throws IOException{
    Speechrec sp1 = new Speechrec();
    textArea = new TextArea(sp1.dr,05,30);
    Button button = new Button("Start speaking");
    // and so on...
}

while (true) {

        Result result = recognizer.recognize();

        if (result != null) {

            String resultText = result.getBestFinalResultNoFiller();

            dr = resultText;

            textArea.setText(resultText);

            // or:

            textArea.append(resultText);
        } 
}

在下一步中,您应该将创建 UI 的所有代码移动到类的构造函数中。那将是一个更清洁的解决方案。

于 2012-04-29T20:28:27.283 回答
0

因为识别器会一直运行,直到您退出程序。如果要更新 textarea,则必须将更新代码设置在可以与 Gui 交互的线程中。例子。

 public void update(String s){
  SwingUtilities.invokeLater(new Runnable(){
         public void run(){
           textArea.append(s);
              }// end run
             });//end Runnable
              }// end method
于 2014-04-22T16:29:01.130 回答