Android TTS doesn't speak

后端 未结 4 650
闹比i
闹比i 2021-01-04 21:20

I am trying to implement text to speech technology of android in my Activity but I face a strange error. I can\'t hear any sound, from my code. The speak method works only i

4条回答
  •  暖寄归人
    2021-01-04 22:11

    After some more hours looking the code, I noticed that the problem is that TTS engine initialization takes some time. If initialization is not over, the speak method call will fail.

    If you "say" something on button click, you will probably won't need this, because user will take some time to think before pressing the button, and the initialization will be over.

    If you want to "say" something as soon initialization finishes, use this code :

    talker = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    
            @Override
            public void onInit(int arg0) {
           if(arg0 == TextToSpeech.SUCCESS) 
               {
            talker.setLanguage(Locale.US);
                say(gameover,true);
                say(line,false);
                say(definition_string,false);
                }
            }
        });
    

提交回复
热议问题