Text to Speech not working as expected

给你一囗甜甜゛ 提交于 2019-11-28 02:00:44
peddy94

You can do something like this:

@Override
public void onInit(int status) {

    if (status == TextToSpeech.SUCCESS) {
        reader.setLanguage(canada);
        reader.setPitch(0.9f);
        Log.e("Init", "Success");
        readerInit = true;

        // wait a little for the initialization to complete
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                // run your code here
                speak();
            }
        }, 400);

    }

    else {
        System.out.println("Something went wrong.");
    }

}

It's not very nice, but it works. I hope somebody will find a better solution...

please take a look to this tutorial.
Basically, it forces the init during the onCreate() method.

// Fire off an intent to check if a TTS engine is installed
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

Then, you will be able to speech any text you want at the start (with out any interaction from the user). And of course, speak will work.

HTH!
Milton

Try to use another constructor for the TextToSpeech class that using the given TTS engine:

TextToSpeech(this,this,"com.google.android.tts");

Instead of:

new TextToSpeech(this, this);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!