2

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?

Mr.D
  • 7,353
  • 13
  • 60
  • 119
  • What is the result of `SpeechRecognizer.isRecognitionAvailable()`? What recognition providers are listed on the device under the Android Voice Search Settings? – brandall Jul 13 '16 at 11:51
  • have you checked if your mic is working? and u r not testing on an Emulator? – M. Usman Khan Dec 01 '16 at 08:02
  • 1
    i give the ANS to how to fix this here is link https://stackoverflow.com/a/46014838/5727285 – Hamza Sep 02 '17 at 14:55

0 Answers0