gotev / android-speech

Android speech recognition and text to speech made easy
http://gotev.github.io/android-speech/
Apache License 2.0
486 stars 157 forks source link

android 13 languages issue #68

Closed rehmanjameel closed 1 year ago

rehmanjameel commented 1 year ago

"No languages are currently available on this device" Language dialog shows this warning. Can you help me how to resolve it?

rehmanjameel commented 1 year ago

Issue is solved by creating the static list of languages:

// static list for android 12+ you can use your own desired languages

public void languagesAbove13() { 

        List<String> list = new ArrayList<String>();
        list.add("en-US");
        list.add("es-ES");
        list.add("iw-IL");
        list.add("ja-JP");
        list.add("hi-IN");

        final CharSequence[] items = list.toArray(new CharSequence[0]);

        new AlertDialog.Builder(MainActivity.this)
                .setTitle("Current language: " + Speech.getInstance().getSpeechToTextLanguage())
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Locale locale;

                        if (Build.VERSION.SDK_INT >= 25) {
                            locale = Locale.forLanguageTag(items[i].toString());
                        } else {
                            String[] langParts = items[i].toString().split("-");

                            if (langParts.length >= 2) {
                                locale = new Locale(langParts[0], langParts[1]);
                            } else {
                                locale = new Locale(langParts[0]);
                            }
                        }

                        Speech.getInstance().setLocale(locale);
                        App.saveDataToSharedPreferences("save_lang", items[i].toString());
                        Toast.makeText(MainActivity.this, "Selected: " + items[i], Toast.LENGTH_LONG).show();
                    }
                })
                .setPositiveButton("Cancel", null)
                .create()
                .show();

    }
rehmanjameel commented 1 year ago

I found the solution by using this method hope it's fine for you to add in your project. @yuripourre