在我的应用程序中,我包含了文本转语音。
但我不知道为什么我的 onInit 没有被调用,因此语言没有改变。我在这里找不到任何适合我的问题的答案。有什么帮助吗?
我的吐司也没有出现。那是我知道它没有进入它的时候。
public void onInit(int initStatus) {
//check for successful instantiation
//Toast.makeText(this, initStatus, Toast.LENGTH_LONG).show();
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE) {
myTTS.setLanguage(Locale.US);
Toast.makeText(this, "Text To Speech ", Toast.LENGTH_LONG).show();
}
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "Sorry! Text-To-Speech failed...", Toast.LENGTH_LONG).show();
}
}
我使用 onItemClickListener 因为我正在使用带有基本适配器的 gridView..
onItemClickListener :
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(this, "Item at pos "+ position +" clicked", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "ItemName : " + getResources().getResourceEntryName(arrImages.get(position)), Toast.LENGTH_LONG).show();
String words = getResources().getResourceEntryName(arrImages.get(position));
String word = db.getPronunciation(words);
speakWords(word);
//int imageID = adapter.arrImages.get(position);
}
口语:
private void speakWords(String speech) {
//implement TTS here
//speak straight away
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
TTS 对象:
//TTS object
private TextToSpeech myTTS;
//status check code
private int MY_DATA_CHECK_CODE = 0;
在 onCreate :
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
在 onActivityResult 中:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
}
else {
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}