Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using TTS(TextToSpeech) in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 277
    Comment on it

    We have a feature called TextToSpeech and is available for Android 1.6 and higher. Usind TTS we can build an app that speaks the text of various languages.

    Below are the steps to do so:

    1. Simply create an xml layout that includes an input filed with a button to speak that.

    2. Create an Activty that implements TextToSpeech.OnInitListener

    3. Then in the Activity, in the onClick() event of that button, fetch the text from input fieldand pass it to tts.speak() method like:

      String text = txtText.getText().toString();
      tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

      tts above is the object of TextToSpeech, forget not to initilize the same before using it in onCreate() method.

    TextToSpeech.OnInitListener has its public method:

    @Override
    public abstract void onInit (int status)
    {
      if (status == TextToSpeech.SUCCESS) {
    
            int result = tts.setLanguage(Locale.US);
    
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TextToSpeech", "Language not supported");
            } else {
                String text = txtText.getText().toString();
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
            }
    
        } else {
            Log.e("TextToSpeech", "Unable to initialize");
        }
    
    }
    

    In the onDestroy() method of the activity, shut down the tts like:

    @Override
        public void onDestroy() {
            if (tts != null) {
                tts.stop();
                tts.shutdown();
            }
            super.onDestroy();
        }

    Thanks and all the best :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: