Android speech To Text Handling
in softkeyboard i have the option for speech to text , when i spoke it show a list of suggestion , when i select a text ,i need to fill my editText with this text, how can i done this i have see SpeechRecognizer class ,i don't know how can i use this ,please help me
SpeechRecognizer rec=SpeechRecognizer.createSpeechRecognizer(context);
RecognitionListener listener = new RecognitionListener() {
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
@Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
@Override
开发者_高级运维 public void onError(int error) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onBeginningOfSpeech() {
}
};
rec.setRecognitionListener(listener);
Assuming your text edit is named "te":
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
StringBuilder sb = new StringBuilder();
for(String p: voiceResults) {
sb.append(p);
sb.append("\n"); } te.setText(sb.toString());
}
Normally, you are only interested in the first result (i.e voiceResults (0)) since that is the most probable match but the code above shows all of them so you can see what is returned.
精彩评论