0

我有一个语音识别系统,这就是listen(button)功能

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       

    // TODO add your handling code here:

    jButton1.setIcon( loading_icon );
    microphone.startRecording()
    jButton1.setIcon( speak_icon );

    System.out.println("Start speaking. Press Ctrl-C to quit.\n");

    result = recognizer.recognize();
    if (result != null) {
         String bestfinalnofiller = result.getBestFinalResultNoFiller();
         String hypothesis = result.getBestPronunciationResult();
         String getresult = result.getBestResultNoFiller();

         System.out.println("You said: " + bestfinalnofiller + '\n');
         System.out.println("You said: " + hypothesis + '\n');
         System.out.println("You said: " + getresult + '\n');

    } else {

    } 
}      

我的结果将是:按钮图标保持不变,并在工作完成后更改系统在工作进行时打印字符串"start speaking..."

recognizer.recognize()功能:

public Result recognize(String referenceText) throws IllegalStateException {

    Result result = null;
    checkState(State.READY);
    try {
        setState(State.RECOGNIZING);
        result = decoder.decode(referenceText);
    } finally {
        setState(State.READY);
    }
    return result;

}

我试图添加:

SwingUtilities.invokeLater

但没有用,请帮助我,我想让按钮在计算时更改它的图标图像......

4

1 回答 1

2

很可能那recognizer.recognize()是一个阻塞的方法,也就是在它完成之前,它不会返回。

这意味着在方法返回之前,您将阻塞事件调度线程,该线程除其他外负责处理绘制请求。

这意味着在您的方法返回之前,无法绘制或更新(或响应)任何内容。

一个简单的解决方案是使用SwinWorker,它允许您设置后台进程以执行阻塞调用,但还提供了许多方法通过 EDT 同步 UI 的任何更新

详细了解Swing和SwimgWorker 中的并发

于 2014-04-07T21:44:08.117 回答