I want to create simple Speech to Text application and I have found SpeechRecognizer
class, which looks like something that I need.
I have added these permissions to my manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Then in my MainActivity
there is only one button
and one textVew
.
I have set my activity's onCreate
like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new MySpeechRecognitionListener());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"com.daniyar.speechrecogniitontest");
sr.startListening(intent);
}
});
}
And In my MySpeechRecognitionListener
looks like this:
private class MySpeechRecognitionListener implements RecognitionListener {
@Override
public void onReadyForSpeech(Bundle bundle) {
Toast.makeText(MainActivity.this, "Ready", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(int i) {
Toast.makeText(MainActivity.this, "Error " + i, Toast.LENGTH_LONG).show();
}
@Override
public void onResults(Bundle bundle) {
String str = "";
ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0 ; i < data.size(); i++) {
str += data.get(i);
}
textView.setText(str);
}
}
When I launch my application and then click to button it shows me toast with Error 5
and in my logs, there is a message:
SpeechRecognizer: no selected voice recognition service
How to set recognition service? Also, is it possible to use SpeechRecognizer offline?