2012-06-25 4 views
3

Android에서 SpeechRecognizer 및 RecognizerIntent를 사용하여 음성 인식 기능을 구현하고 있습니다. 내 목표는 음성 인식기가 결과를 화면에 표시 한 후 음성 듣기를 다시 시작하는 것입니다. 이를 위해 다음 코드를 사용하고 있습니다.SpeechRecognizer가 첫 번째 결과 후 청력을하지 못합니다.

문제는 첫 번째 시간이 정상적으로 실행되어 결과를 표시하지만 두 번째 시간 (onResults 메서드에서 호출 됨)을 청취 한 후 몇 가지 이유로 음성을 듣지 못하는 것입니다. 그런 다음 ERROR_SPEECH_TIMEOUT 오류가 발생합니다. 즉, 음성 입력이 없음을 의미합니다. Logcat에서 onReadyForSpeech()를 입력하는 것을 볼 수는 있지만 어쨌든, 내가 말하는 것을 듣지 못합니다.

왜 이런 일이 일어날 지 알고 계십니까? 결과를 반환 한 후에도 계속 듣고 있습니까? startListening을 명시 적으로 다시 호출하는 것이 맞습니까?

public class VR extends Activity implements RecognitionListener { 


    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 
    private TextView vrtext; 
    private SpeechRecognizer speech = null; 
    private Intent intent; 
    private String TAG = "VR"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.vr); 

     vrtext = (TextView) findViewById(R.id.vrtext); 

    } 

    @Override 
    public void onResume() 
    { 
     listen(); 
     super.onResume(); 
    } 

    private void listen() 
    { 
     speech = SpeechRecognizer.createSpeechRecognizer(this); 
     speech.setRecognitionListener(this); 
     intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en"); 
     intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName()); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 
     intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); 

     speech.startListening(intent); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     // TODO Auto-generated method stub 

     if(speech != null) 
     { 
      speech.destroy(); 
      Log.i(TAG,"destroy"); 
     } 

    } 

    public void onBeginningOfSpeech() { 
     // TODO Auto-generated method stub 
     Log.i(TAG, "onbeginningofspeech"); 
    } 

    public void onBufferReceived(byte[] arg0) { 
     // TODO Auto-generated method stub 
     //Log.i(TAG, "onbufferreceived"); 
    } 

    public void onEndOfSpeech() { 
     // TODO Auto-generated method stub 
     Log.i(TAG, "onendofspeech"); 
    } 

    public void onError(int arg0) { 
     // TODO Auto-generated method stub 
     Log.i(TAG, "error code: " + arg0); 
    } 

    public void onEvent(int arg0, Bundle arg1) { 
     // TODO Auto-generated method stub 
     Log.i(TAG, "onevent"); 
    } 

    public void onPartialResults(Bundle arg0) { 
     // TODO Auto-generated method stub 
     Log.i(TAG, "onpartialresults"); 
    } 

    public void onReadyForSpeech(Bundle arg0) { 
     // TODO Auto-generated method stub 
     Log.i(TAG, "onreadyforspeech"); 
    } 

    public void onResults(Bundle arg0) { 
     // TODO Auto-generated method stub 
     Log.i(TAG, "onresults"); 
     ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     String s = ""; 
     for (String result:matches) 
      s += result + "\n"; 

     vrtext.setText(s); 

     speech.startListening(intent); 

    } 

    public void onRmsChanged(float arg0) { 
     // TODO Auto-generated method stub 
     //Log.i(TAG, "onrmschanged"); 
    } 

} 
+0

이전 스피치 개체를 청소하지 않고 '수신 대기'를하고 있습니다. 듣기 메서드를 호출하기 전에 음성 객체에서'destroy()'를 더 잘 호출하십시오. –

답변

2

"결과를 반환 한 후에도 계속 듣고 있습니까?" 아니요

"startListening을 다시 명시 적으로 호출 할 수 있습니까?" 예. 당신이 지속적으로 일어나고 인식을 유지하려면 몇 가지 오류는 다음과 같이 발생하는 경우

또한, 다시 startListening를 호출해야합니다 :

@Override 
public void onError(int errorCode) 
{ 
    if ((errorCode == SpeechRecognizer.ERROR_NO_MATCH) 
      || (errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)) 
    { 
     Log.d(TAG, "didn't recognize anything"); 
     // keep going 
     recognizeSpeechDirectly(); 
    } 
    else 
    { 
     Log.d(TAG, 
       "FAILED " 
         + SpeechRecognitionUtil 
           .diagnoseErrorCode(errorCode)); 
    } 
} 

체크 아웃 특정 음성 단어 here을 감지 SpeechRecognizer를 사용하여 내 코드입니다.

+0

고마워요. 이제 작동합니다! – Erol

+0

startReadening()을 호출 한 후 onReadyForSpeech()를 호출하지 않은 경우 문제가 발생 했습니까? startListening()을 몇 번 호출 한 후이 문제가 발생합니다. 그리고 그 무작위로 일어납니다. – Mike6679

+0

예이 오류가 표시되고 그것이 내 잘못인지 또는 Android 버그인지 알 수 없습니다. – user798719

1

활동 내에 하나의 SpeechRecognizer 개체를 사용해야합니다. 빠르고 더러운 방법은 정적으로 만드는 것입니다.

private static SpeechRecognizer speech = null; 

listen() 메서드를 변경하여 음성 개체에서 null을 확인하십시오.

private void listen() 
{ 
    if (speech == null) { 
     speech = SpeechRecognizer.createSpeechRecognizer(this); 
     speech.setRecognitionListener(this); 
    } 
    intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en"); 
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName()); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); 

    speech.startListening(intent); 
} 

전화는 onResults()onError()의 방법을들을 수 있습니다.

public void onResults(Bundle arg0) { 
    // TODO Auto-generated method stub 
    Log.i(TAG, "onresults"); 
    ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
    String s = ""; 
    for (String result:matches) 
     s += result + "\n"; 

    vrtext.setText(s); 

    //speech.startListening(intent); 
    listen(); 

} 

public void onError(int arg0) { 
    // TODO Auto-generated method stub 
    Log.i(TAG, "error code: " + arg0); 
    listen(); 
} 

마지막으로 onDestroy()에서 필요한 청소를하는 것을 잊지 마세요.

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    speech.destroy(); 
} 
관련 문제