Code example for TextToSpeech

Methods: getFeaturesgetLanguageisLanguageAvailablespeak

1
        } 
        return null; 
    } 
 
    private boolean isNetworkRequiredForSynthesis() { 
        Set<String> features = mTts.getFeatures(mTts.getLanguage());
        return features.contains(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS) &&
                !features.contains(TextToSpeech.Engine.KEY_FEATURE_EMBEDDED_SYNTHESIS);
    } 
 
    private void onSampleTextReceived(int resultCode, Intent data) {
        String sample = getDefaultSampleString();
 
        if (resultCode == TextToSpeech.LANG_AVAILABLE && data != null) {
            if (data != null && data.getStringExtra("sampleText") != null) {
                sample = data.getStringExtra("sampleText");
            } 
            if (DBG) Log.d(TAG, "Got sample text: " + sample);
        } else { 
            if (DBG) Log.d(TAG, "Using default sample text :" + sample);
        } 
 
        if (sample != null && mTts != null) {
            // The engine is guaranteed to have been initialized here 
            // because this preference is not enabled otherwise. 
 
            final boolean networkRequired = isNetworkRequiredForSynthesis();
            if (!networkRequired || networkRequired &&
                    (mTts.isLanguageAvailable(mTts.getLanguage()) >= TextToSpeech.LANG_AVAILABLE)) {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Sample");
 
                mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, params);
            } else { 
                Log.w(TAG, "Network required for sample synthesis for requested language");
                displayNetworkAlert(); 
            } 
        } else {