Text to speech in android?

deepak - Sun Jan 29 2012 12:53:58 GMT-0500 (EST)

How to do text to speech in android?

How to do text to speech in android?

Search for: How to do text to speech in android?

deepak - Sun Jan 29 2012 12:56:11 GMT-0500 (EST)

Very nice article

Very nice article

deepak - Sun Jan 29 2012 13:19:57 GMT-0500 (EST)

Step by step details of how to do it

Step by step details of how to do it

deepak - Sun Jan 29 2012 18:03:49 GMT-0500 (EST)

Step 1 : Find out whether the device supports TTS


1) Find out whether the device supports TTS(text to speech)

 /**Make intent call to check whether the device
  * supports TTS. startActivityForResult is the
  * call back method.
  */
private int MY_DATA_CHECK_CODE = 0;
 Intent checkIntent = new Intent();
 checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
 startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

 protected void onActivityResult
    (int requestCode, int resultCode, Intent data) {

    if (requestCode == MY_DATA_CHECK_CODE) {

        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) 
        {

           // success, create the TTS instance

            tts = new TextToSpeech(this, this);

        }

       else {

            // missing data, install it

            Intent installIntent = new Intent();

            installIntent.
               setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);

            startActivity(installIntent);

        }

    }
  }

deepak - Sun Jan 29 2012 21:01:53 GMT-0500 (EST)

Step 2: Making sure the Initialization was Successful


2) Make sure the initialization was Successful

/**
*Before using the TTS engine, we have to be sure that 
*it has properly been initialized. 
*In order to get informed on whether this has happened, 
*we can implement an interface called OnInitListener. 
*The method onInit will be invoked when the engine 
*initialization has completed with the accompanying status.
*
**/
public void onInit(int status) {       

    if (status == TextToSpeech.SUCCESS) {

        Toast.makeText(TtsActivity.this,

                "Text-To-Speech engine is initialized",
                 Toast.LENGTH_LONG).show();

    }

   else 
   if (status == TextToSpeech.ERROR) {

        Toast.makeText(TtsActivity.this,

       "Error occurred while initializing Text-To-Speech engine",
        Toast.LENGTH_LONG).show();

    }
}

deepak - Sun Jan 29 2012 21:08:52 GMT-0500 (EST)

Step 3: Use the initialized TextToSpeech object for tts


3) Use the initialized TTS object for Text to Speech functionality.

String text = "Welcome Deepak";
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

deepak - Sun Jan 29 2012 21:14:34 GMT-0500 (EST)

Setting the Locale


mTts.setLanguage(Locale.US);