为什么我的onInit没有被调用?

时间:2015-09-11 14:59:49

标签: android android-studio text-to-speech google-text-to-speech

在我的申请中,我已经包含了文字转语音。

但我不知道为什么我的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);
}

speakWords:

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;

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

0 个答案:

没有答案
相关问题