Code example for TextToSpeech

Methods: getDefaultEngine, setLanguage, speak

0
    private void notifyUserDestinationReached(){ 
        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener(){
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    System.out.println("Radde123 SUCCESS ");
                    System.out.println("Radde123 engine: " + tts.getDefaultEngine());
 
                    int result = tts.setLanguage(Locale.US);
 
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        System.out.println("Radde123 This Language is not supported");
                    } else { 
                        AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
 
                        //If we do not have headset just vibrate 
                        if (!amanager.isWiredHeadsetOn()){
                            System.out.println("Radde123 isWiredHeadsetOn");
                            int dot = 200;      // Length of a Morse Code "dot" in milliseconds
                            int dash = 500;     // Length of a Morse Code "dash" in milliseconds
                            int short_gap = 200;    // Length of Gap Between dots/dashes
                            int medium_gap = 500;   // Length of Gap Between Letters
                            int long_gap = 1000;    // Length of Gap Between Words
                            long[] pattern = {
                                    0,  // Start immediately 
                                    dot, short_gap, dot, short_gap, dot,    // s
                                    medium_gap,
                                    dash, short_gap, dash, short_gap, dash, // o
                                    medium_gap,
                                    dot, short_gap, dot, short_gap, dot,    // s
                                    long_gap
                            }; 
 
                            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                            v.vibrate(pattern,-1);
 
                        } else if (amanager.isWiredHeadsetOn() && amanager.isMusicActive()){
                            System.out.println("Radde123 music active");
                            //Turn off music 
                            Intent intent = new Intent("com.android.music.musicservicecommand.togglepause");
                            getApplicationContext().sendBroadcast(intent);
 
                            amanager.setStreamVolume(AudioManager.STREAM_MUSIC,12,0);
 
                            result = tts.speak("You have arrived at your final destination",
                                    TextToSpeech.QUEUE_FLUSH, null);
 
                            if (result == TextToSpeech.ERROR)
                                System.out.println("Radde123 speach failed");
 
                        } else { 
                            amanager.setStreamVolume(AudioManager.STREAM_MUSIC,12,0);
                            result = tts.speak("You have arrived at your final destination",
                                    TextToSpeech.QUEUE_FLUSH, null);
 
                            if (result == TextToSpeech.ERROR)
                                System.out.println("Radde123 speach failed");
                        }