TextToSpeech:不赞成在API Level 21中使用说话function

我尝试在我的应用程序中使用TextToSpeech,

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

但是,在API Level 21中弃用了函数speak(String text,int queueMode,HashMap params)。取而代之,build议使用speak(CharSequence文本,int queueMode,Bundle params,String utteranceId)。 但我不知道如何设置它。 谢谢

Related of "TextToSpeech:不赞成在API Level 21中使用说话function"

 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); } 

答案取自https://stackoverflow.com/a/28000527/4211264

 if (Build.VERSION.RELEASE.startsWith("5")) { tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null, null); } else { tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null); } 

这是为我工作的完整工作

 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); } } });