Ankur Shinde
Ankur Shinde

Reputation: 293

Text to Speech is not working in API level 30 other than English language

I implemented the text to speech in language "Hindi" Its an Indian language my application which is working fine till the API level 29. Its working fine for English but not for the Hindi. But in new devices which are of API level 30, it's not working. in debugging its giving result value -2 "language no supported error" in API level 30 devices.

private void setTextTospeech() {
    textToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                if (language.toLowerCase().contains(langaugeCodeEnglish)) {
                    int result = textToSpeech.setLanguage(new Locale("en", "IN"));
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        //Toast.makeText(mContext, result + " is not supported", Toast.LENGTH_SHORT).show();
                        Log.e("Text2SpeechWidget", result + " is not supported");
                    }
                } else {
                    int result = textToSpeech.setLanguage(new Locale("hi", "IN"));
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            textToSpeech.setLanguage(Locale.forLanguageTag("hin"));
                        } else {
                            //  Toast.makeText(mContext, result + "Language is not supported", Toast.LENGTH_SHORT).show();
                            Log.e("Text2SpeechWidget", result + "Language is not supported");
                        }
                        Log.e("Text2SpeechWidget", result + " is not supported");
                    }
                }
            }
        }
    });
}


private void speak(String s, String text) {
        try{
            float pitch = (float) 0.62;
            float speed = (float) 0.86;
            textToSpeech.setSpeechRate(speed);
            textToSpeech.setPitch(pitch);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Bundle bundle = new Bundle();
                bundle.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_MUSIC);
                textToSpeech.speak(s, TextToSpeech.QUEUE_FLUSH, bundle, null);
                textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, bundle, null);
            } else {
                HashMap<String, String> param = new HashMap<>();
                param.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
                textToSpeech.speak(s, TextToSpeech.QUEUE_FLUSH, param);
                textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, param);
            }
        }catch (Exception ae){
            ae.printStackTrace();
        }
    }

As per new docs. I also add a queries tag inside manifest tag.

<queries>
   ...
  <intent>
      <action android:name="android.intent.action.TTS_SERVICE" />
  </intent>
 </queries>

Upvotes: 6

Views: 2027

Answers (4)

Happy Lee
Happy Lee

Reputation: 179

I had the same problem, here is my experience.

I have 3 test devices. One of them is a Pixel 3A(Android11), in which Text-to-Speech function works well without declaring "TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE" in the queries elements of the manifest file.

But Text-to-Speech function didn't work on my RedMi K30Pro(Android12) and RedMi K50 Ultra(Android12), unless I declare "TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE" in the manifest file.

Upvotes: 0

Ayushi Khandelwal
Ayushi Khandelwal

Reputation: 336

Worked without these permissions also. Tested on Android 10, 11 and 12.

Upvotes: 0

John Shiveley
John Shiveley

Reputation: 77

Thank you Mustafa Balin

android studio made me change it a bit though.

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />

Upvotes: 1

Mustafa Balin
Mustafa Balin

Reputation: 39

Add the following permission in the manifest file;

uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"

Upvotes: 3

Related Questions