0

I am using TextToSpeak in one of my app. While adding the speech string to the TextToSpeech Object Instance, It doesn't produce any sound. Though I am not getting any error in the log but still sound is not coming out. I want the device to speak as the value of a textview gets Set by resultMSG.setText() method.

public class ResultActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        ............
        resultMSG.setText(resultMSG_STR);
        textToSpeech = new TextToSpeech(getApplicationContext(),newTextToSpeech.OnInitListener(){

            @Override
            public void onInit(int status) {
                System.out.println("Txt To Speech STATUS = "+status);
                System.out.println("Txt To Speech Error STATUS = "+TextToSpeech.ERROR);
                System.out.println("Txt To Speech Success STATUS = "+TextToSpeech.SUCCESS);
                if(status == TextToSpeech.SUCCESS){
                    textToSpeech.setLanguage(Locale.ENGLISH);
                }
            }
        });
textToSpeechResult = textToSpeech.speak(resultMSG_STR,TextToSpeech.QUEUE_ADD,null);
    }
}
Akash Sethiya
  • 73
  • 1
  • 4
  • `OnInit` is asynchronous, so your `speak` statement will be most likely be called prior to the initialisation. As a test, move your `speak` statement into `onInit`, after `setLanguage` - it should work from there. However, your code shows misunderstanding of basic fundamentals. You would do well to read a few more tutorials. – brandall Sep 12 '16 at 21:14
  • Thanks brandall. This works for me. But still, there is a delay of around 4-5 seconds. The speech started after 4-5 seconds of activity launching. – Akash Sethiya Sep 14 '16 at 03:42
  • That's unfortunately a common delay period - check out this post http://stackoverflow.com/q/36013611/1256219. You should initialise your TTS engine separately, so you can reuse it without having to reinitialise it every time. – brandall Sep 15 '16 at 06:56

1 Answers1

0

Check that your volume is up loud enough to hear. You may also want to install an app from the Play store to verify that TTS is working on your phone.

You might also want to move your code out of the onCreate method and into the onResume method.

Looking over my own code, I notice that I have that "speak" call inside the init callback. I'm thinking your "speak" call is getting called before your TTS is fully initialized.

Mick
  • 676
  • 5
  • 12