3

The app I am building requires me to speak out loud, using the inbuilt TextToSpeech algorithm, a random string from almost any language without using the Internet. I am not sure how to go about this.

Below is what I have so far, but it does not work with languages which are not similar to English, e.g. Chinese. I believe this is due to the Locale that is set to US.

t1=new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.US); // TODO: 22/09/2015 FIX LOCALE FOR TextToSpeech
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        /** creates a high quality voice when on Lollipop or better android version */
                        Voice voice = new Voice("voice", Locale.US, Voice.QUALITY_VERY_HIGH, Voice.LATENCY_NORMAL, false, null);
                        t1.setVoice(voice);
                    }
                }
            }
        });

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv = (TextView) v.findViewById(R.id.to_language);
                String toSpeak = tv.getText().toString();
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null, v.getTag().toString());

                    t1.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                        @Override
                        public void onStart(String utteranceId) {
                           //tv.setTextColor(Color.parseColor("#57d801"));
                        }

                        @Override
                        public void onDone(String utteranceId) {
                            //tv.setTextColor(Color.parseColor("#FFFFFF"));
                        }

                        @Override
                        public void onError(String utteranceId) {

                        }
                    });
                } else {
                    t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                    t1.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
                        @Override
                        public void onUtteranceCompleted(String utteranceId) {

                        }
                    });
                }
            }
        });

If anyone could point me in the right direction for a solution to my problem it would be greatly appreciated.

Travis Knight
  • 301
  • 1
  • 4
  • 18
  • Check [this](http://stackoverflow.com/questions/23348954/android-get-device-locale) link . I hope you will find your answer. – D_Alpha Oct 09 '15 at 07:16
  • @DeepanshuHarbola Thank you! Unfortunately I can not use the device Locale. It needs to be fully based off the given text, whichever language that may be. – Travis Knight Oct 09 '15 at 09:46
  • Is it possible to do some sort of search Locally on the device (no internet) for a compatible TTS Locale to suit a specific country? Country name is available offline in the app. Also does anyone know how the compatibility of TTS in various languages over Android devices? Sorry for all the questions. Trying to work out the solution to my question above. – Travis Knight Oct 12 '15 at 02:00
  • 1
    Don't know about the TTS part, but for language detection you could probably use something like https://github.com/optimaize/language-detector – Johan Walles Apr 13 '16 at 10:01
  • @JohanWalles, 74 mb ! – Noor Hossain Feb 14 '21 at 06:51
  • Turns out that nowadays in Android 10 / API level 29 (released September 2019), language detection is built in. I have not tried this, but it does exist: https://developer.android.com/reference/android/view/textclassifier/TextClassifier#detectLanguage(android.view.textclassifier.TextLanguage.Request) – Johan Walles Feb 15 '21 at 07:37

0 Answers0