自动检测TextToSpeech Android的区域设置/语言

时间:2015-10-09 06:30:25

标签: android locale translation text-to-speech

我正在构建的应用程序要求我大声说出来,使用内置的TextToSpeech算法,这是一种几乎任何语言的随机字符串,无需使用Internet。我不知道该怎么做。

以下是我目前的情况,但它不适用于与英语不相似的语言,例如:中文。我相信这是由于设置为美国的Locale。

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) {

                        }
                    });
                }
            }
        });

如果有人能指出我正确的方向来解决我的问题,我们将不胜感激。

0 个答案:

没有答案