Search code examples
androidspeech-to-text

unable to use android Speech to text


I want to use android's speech to text. following is my MainActivity.java

public class MainActivity extends AppCompatActivity {

private TextView txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

    // hide the action bar
    //getActionBar().hide();

    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i("OnClickListener CALLED:", "");
            promptSpeechInput();
        }
    });

}


private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
    Log.i("promptSpeechInput :", "Prompt speech CALLED");
}

/**
 * Receiving speech input
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.i("OnActivity CALLED1:","one");
    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                txtSpeechInput.setText(result.get(0));
                Log.i("OnActivity CALLED2:", result.get(0));
            }
            break;
        }

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Here is my Log:

   02-26 16:23:05.047 13636-13636/com.example.vishal.speechtotext I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@39ca1da6 time:158833044
   02-26 16:23:09.637 13636-13636/com.example.vishal.speechtotext I/Timeline: Timeline: Activity_launch_request time:158837632
02-26 16:23:09.707 13636-13636/com.example.vishal.speechtotext I/promptSpeechInput :: Prompt speech CALLED
02-26 16:23:42.797 13636-13636/com.example.vishal.speechtotext I/OnActivity CALLED1:: one
02-26 16:23:42.837 13636-13636/com.example.vishal.speechtotext I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@39ca1da6 time:158870837
  • code just runs, doesn't show any error. i am unable to find what the error is.

  • I am testing on Lollipop

  • Do i need net connection?

    any help will be appreciated. enter image description here


Solution

  • Yes you'll be needing Internet for this. Paste Following permissions inside your manifest file:

    <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    Right now all the devices are not supporting offline speech input. To enable offline speech input for supported devices follow below steps:

    1. On your device go to Settings -> Language and Input. Click on icon on Google voice input.
    2. Under ALL tab select the language you want to download. 3. Once the language package downloaded, you can see it under INSTALLED tab.

    I have downloaded speech input packages on my Nexus 5 and offline speech is working fine.

    enter image description here

    For further reference checkout the below link:

    https://web.archive.org/web/20160218040751/http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/