Search code examples
androidspeech-to-text

Google Speech to text throwing java.lang.ClassCastException


** * Showing google speech input dialog * */

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();
        }
    }

Below exception:

 03-04 16:33:13.281 W/Bundle  ( 3741): Key android.speech.extra.LANGUAGE expected String but value was a java.util.Locale.  The default value <null> was returned.
    03-04 16:33:13.281 W/Bundle  ( 3741): Attempt to cast generated internal exception:
    03-04 16:33:13.281 W/Bundle  ( 3741): java.lang.ClassCastException: java.util.Locale cannot be cast to java.lang.String
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.os.BaseBundle.getString(BaseBundle.java:921)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.content.Intent.getStringExtra(Intent.java:4822)
    03-04 16:33:13.281 W/Bundle  ( 3741): at ihn.<init>(PG:97)
    03-04 16:33:13.281 W/Bundle  ( 3741): at com.google.android.voicesearch.intentapi.IntentApiActivity.onCreate(PG:37)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.Activity.performCreate(Activity.java:5953)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.access$800(ActivityThread.java:148)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.os.Handler.dispatchMessage(Handler.java:102)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.os.Looper.loop(Looper.java:135)
    03-04 16:33:13.281 W/Bundle  ( 3741): at android.app.ActivityThread.main(ActivityThread.java:5312)
    03-04 16:33:13.281 W/Bundle  ( 3741): at java.lang.reflect.Method.invoke(Native Method)
    03-04 16:33:13.281 W/Bundle  ( 3741): at java.lang.reflect.Method.invoke(Method.java:372)
    03-04 16:33:13.281 W/Bundle  ( 3741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

Solution

  • Try hard coding language like

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
    

    instead of

    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    

    check whether it works or not, if it works then you its the problem with the Locale.getDefault());

    your app can query for the list of supported languages by sending a RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS ordered broadcast like so:

    Intent detailsIntent =  new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
        sendOrderedBroadcast(
                detailsIntent, null, new LanguageDetailsChecker(), null, Activity.RESULT_OK, null, null);
    

    You can write the broadcast reciever like

    public class LanguageDetailsChecker extends BroadcastReceiver
    {
        private List<String> supportedLanguages;
    
        private String languagePreference;
    
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Bundle results = getResultExtras(true);
            if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE))
            {
                languagePreference =
                        results.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
            }
            if (results.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES))
            {
                supportedLanguages =
                        results.getStringArrayList(
                                RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
            }
        }
    }
    

    Check this link out toolkit for using Android's Sensing capabilities