Search code examples
javaandroidmediaspeech-recognitionmicrophone

How to detect if speech to text is available on android?


I believe I have figured out how to detect if an android device has a microphone, like so:

Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0);
        TextView micAvailView = (TextView) findViewById(R.id.mic_available_flag);
        if (speechActivities.size() != 0) { //we have a microphone

        }
        else { //we do not have a microphones

        }

However, how does one detect whether the android device has speech-to-text capability? Or should the above be used to detect that? If so, how does one detect if the device has a microphone?

Any feedback is appreciated, thanks.


Solution

  • The code you attached is indeed used to detect if audio recognition is available [1]:

    // Check to see if a recognition activity is present
    PackageManager pm = getPackageManager();
    List activities = pm.queryIntentActivities(
      new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() != 0) {
      speakButton.setOnClickListener(this);
    } else {
      speakButton.setEnabled(false);
      speakButton.setText("Recognizer not present");
    }
    

    To test if a mic is present, just follow the code in [2] and the docs at [3], when calling prepare() you should get an IOException if the mic is not available:

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();
    

    [1] http://developer.android.com/resources/articles/speech-input.html
    [2] http://developer.android.com/guide/topics/media/index.html#capture
    [3] http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html