Code example for TextToSpeech

Methods: setLanguagespeak

    public void onInit(int status) {
        // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR. 
        if (status == TextToSpeech.SUCCESS) {
            // Set preferred language to US english. 
            // Note that a language may not be available, and the result will indicate this. 
            int result = mTts.setLanguage(Locale.US);
            // Try this someday for some interesting results. 
            // int result mTts.setLanguage(Locale.FRANCE); 
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
               // Lanuage data is missing or the language is not supported. 
                Log.e(TAG, "Language is not available.");
            } else { 
                // Check the documentation for other possible result codes. 
                // For example, the language may be available for the locale, 
                // but not for the specified country and variant. 
                // The TTS engine has been successfully initialized. 
                // Allow the user to press the button for the app to speak again. 
                // Greet the user. 
        } else { 
            // Initialization failed. 
            Log.e(TAG, "Could not initialize TextToSpeech.");
    private static final Random RANDOM = new Random();
    private static final String[] HELLOS = {
      "What's crack-a-lackin?", 
      "That explains the stench!" 
    private void sayHello() { 
        // Select a random hello. 
        int helloLength = HELLOS.length;
        String hello = HELLOS[RANDOM.nextInt(helloLength)];
            TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.