Android TextToSpeech:API级别21中已弃用的speak函数
我尝试在我的应用程序中使用TextToSpeechAndroid TextToSpeech:API级别21中已弃用的speak函数,android,text-to-speech,Android,Text To Speech,我尝试在我的应用程序中使用TextToSpeech String text = editText.getText().toString(); tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 但是函数speak(字符串文本、int-queueMode、HashMap-params)在API级别21中被弃用。相反,建议使用speak(CharSequence text、int queueMode、Bundle参数、String outtanceid
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
但是函数speak(字符串文本、int-queueMode、HashMap-params)在API级别21中被弃用。相反,建议使用speak(CharSequence text、int queueMode、Bundle参数、String outtanceid)。
但我不知道如何设置它。谢谢这是我的全部作品
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);
}
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);
}
}
});
当我使用你的建议时,Android Studio会说:“params=“null”utranceId=“null”并展开if语句建议。而且,这对我的案例没有足够的帮助。Build.VERSION\u代码现在已经过时(2021年4月)。只需替换上面的条件(Android.OS.Build.VERSION.SdkInt>=Android.OS.BuildVersionCodes.Lollipop)