如何在 Android 中将此 TOAST 消息转换为语音?
示例
Toast.makeText(MainActivity.this, "I am enter code here" +positive[+ position]+ " always", Toast.LENGTH_SHORT).show();
最佳答案
先导入包
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);
}
糟糕。我忘了,你还需要定义一个 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
来调用这个函数,使用一个按钮。将其修改为您想要在 toast 消息时调用此函数的方式。
只需将字符串 text
作为您的 toast 消息。在上面的示例中,txtText
是一个 editText。根据您的要求修改
关于android - 将 Toast 消息转换为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25778212/