Reputation: 403
I have a maind UI class that has button which instantiate a class that is implementing SpeechRecognizer library to convert speech from text.
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startNoiseProcessService();
}
});
startNoiseProcessService() is a function which is calling another class object.
public void startNoiseProcessService() {
StreamService ss = new StreamService(this);
String s = ss.startStreaming();
if ( s.equals("NA") ) {
Toast t = Toast.makeText(getApplicationContext(),threadCnt +
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
StreamService class implements SpeechRecognizer API.
This StreamService class is getting called perfectly fine and problem lies is I am unable to get the converted text from speech at the time after results are calculated by onResults(Bundle)
method.
I need the converted text to update my textbox and I am not getting it.
startStreaming() implementation:
public String startStreaming() {
text = "";
if ( !SpeechRecognizer.isRecognitionAvailable(context) ) {
text = "NA";
return text;
}
Log.d(TAG, "started taking input");
sr = SpeechRecognizer.createSpeechRecognizer(context);
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
sr.setRecognitionListener( new mylistener());
sr.startListening(intent);
}
Upvotes: 0
Views: 341
Reputation: 7975
Based on your comment, I think you can't have the results at the time sr.startListening(intent)
is called. You need to get the results using the onResults
method of the listener and process the text that comes inside the Bundle
from there.
The way I implemented this was to have a Service
in charge of handling the SpeechRecognizer
. Then when onResults
is called I send the text back to the original caller using a message handler. You can see details on how to do this here: https://developer.android.com/training/multiple-threads/communicate-ui.html
Upvotes: 1