TextToSpeech : deprecated speak function in API Level 21

吃可爱长大的小学妹 提交于 2020-06-24 19:35:23

问题


I try to use a TextToSpeech in my app,

String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

But the function speak(String text, int queueMode, HashMap params) is deprecated in API Level 21. Instead of that, it is adviced to use speak(CharSequence text, int queueMode, Bundle params, String utteranceId). But I don't know how to set it. Thanks


回答1:


String text = editText.getText().toString();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    tts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
} else {
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}



回答2:


Here is the complete work that work for me

Private TextToSpeech ts
 ts=new TextToSpeech(CurrentActivity.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                String text = "Any Text to Speak";
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
                } else {
                    ts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                }

            }
        });


来源:https://stackoverflow.com/questions/30706780/texttospeech-deprecated-speak-function-in-api-level-21

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!