2014-07-25 1 views
1

AWT 프레임 워크를 사용하여 기본 음성 인식 Java API를 만들었습니다. 코드 작성이 끝났습니다. API도 실행 중이지만 인식이 진행되는 동안 API에 Stop 버튼을 추가하고 싶습니다. 음성 인식을 시작한 후에이 중지 단추 사용을 설정하려고합니다. 하지만 그렇게 할 수는 없습니다. Stop 버튼을 클릭하면 인식의 while 루프를 깨고 무한대로 루프를 보내지 않고 싶습니다. 은 당신의 제안을 저를 도와주세요 .. 내 주요 클래스는 다음과 같습니다자바 음성 인식 스핑크스 API 루프에서 while 버튼을 사용할 수 있습니다.

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); 

    } 
} 

답변

0

당신은 당신의 주에서 public static boolean isStart;을 만들고 그것을 참조 할 수 있습니다 while 루프.

while(true) 대신에 while(main.isStart)과 같은 것을 사용할 수 있습니다. 그런 다음 중지 버튼을 누르면 isStart = false;으로 설정하고 시작 버튼을 누르면 isStart = true;으로 설정하십시오.

항상 코드에서 while(true)을 사용하지 않는 것이 좋습니다. 더 좋은 방법이있을 것입니다.

+0

답장을 보내 주셔서 감사합니다. 하지만 실제로는 '말하기'버튼을 클릭하면 API UI가 중단됩니다. 어떤 버튼도 누르지 않을 때까지 어떤 버튼도 반응하지 않고이 while 루프에서 빠져 나오지 않는 한. – user3703157

+0

UI가 말하기 프로세스에서 멈추는 경우 별도의 스레드에서 실행하고 비동기 적으로 실행해야 할 수 있습니다. 비동기 작업을 사용하려면 http://developer.android.com/reference/android/os/AsyncTask.html을 참조하십시오. –