如何在Android中将此TOAST消息转换为语音?
示例
Toast.makeText(MainActivity.this, "I am enter code here" +positive[+ position]+ " always", Toast.LENGTH_SHORT).show();
答案 0 :(得分:1)
首先导入包
import android.speech.tts.TextToSpeech;
然后初始化
private TextToSpeech tts;
tts = new TextToSpeech(this, this);
最后制作一个这样的函数
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
Woops。我忘记了,你还需要定义一个onInit函数
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
在这个例子中,我使用一个onClickListener
来调用这个函数,使用一个按钮。将其修改为您在祝酒消息时调用此函数的方式。
只需将字符串text
作为您的Toast消息即可。在上面的示例中,txtText
是一个editText。根据您的要求修改
答案 1 :(得分:0)
TextView wordView = (TextView)view; String wordChosen = (String) wordView.getText(); Toast.makeText(MainActivity.this, "I am " + positive[+position] + " always", Toast.LENGTH_SHORT).show(); //tts.speak("I am" + blank +position+" always", TextToSpeech.QUEUE_FLUSH, null); tts.speak("You chose, you are "+wordChosen+" today and always", TextToSpeech.QUEUE_FLUSH, null);