2012年2月19日日曜日

SpeechRecognizer

I check SpeechRecognizer class. This class is Speech Recognizing class.
How to get instance is call createSpeechRecognizer(Context);
SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
and make listener class,
class MyRecognitionListener implements RecognitionListener {

 @Override
 public void onBeginningOfSpeech() {
  Log.d("Speech", "onBeginningOfSpeech");
 }

 @Override
 public void onBufferReceived(byte[] buffer) {
  Log.d("Speech", "onBufferReceived");
 }

 @Override
 public void onEndOfSpeech() {
  Log.d("Speech", "onEndOfSpeech");
 }

 @Override
 public void onError(int error) {
  Log.d("Speech", "onError");
 }

 @Override
 public void onEvent(int eventType, Bundle params) {
  Log.d("Speech", "onEvent");
 }

 @Override
 public void onPartialResults(Bundle partialResults) {
  Log.d("Speech", "onPartialResults");
 }

 @Override
 public void onReadyForSpeech(Bundle params) {
  Log.d("Speech", "onReadyForSpeech");
 }
 

 @Override
 public void onResults(Bundle results) {
  Log.d("Speech", "onResults");
  ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
  for (int i = 0; i < strlist.size();i++ ) {
   Log.d("Speech", "result=" + strlist.get(i));
  }
 }

 @Override
 public void onRmsChanged(float rmsdB) {
  Log.d("Speech", "onRmsChanged");
 }
 
}
the important method is onResults. There is recognized word in "results".
public void onResults(Bundle results) {
 Log.d("Speech", "onResults");
 ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
 for (int i = 0; i < strlist.size();i++ ) {
  Log.d("Speech", "result=" + strlist.get(i));
 }
}
I make the listener.
MyRecognitionListener listener = new MyRecognitionListener();
I set listener.
sr.setRecognitionListener(listener);
I start speech recognizing.
sr.startListening(RecognizerIntent.getVoiceDetailsIntent(getApplicationContext()));
onResults() call only once.
I said "english".
logcat printed.
result=リング
result=english
result=ring
result=イング
result=ing
It is OK!
I must have android.permission.RECORD_AUDIO. and network is enable. source code is here. Please suggest your android issue by twitter or mail, I would try to clear up your issue.

1 件のコメント:

  1. Thank you for posting this. This is the only good example I've been able to find for SpeechRecognizer.

    返信削除