Android TextToSpeech - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android TextToSpeech class.

This code example shows how to use the following methods:getFeatures, getLanguage, isLanguageAvailable, speak
        } 
        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);

8
CodeRank
This code example shows how to use the following methods:
 
        return false; 
    } 
 
    private void initTTS() { 
        mTts = new TextToSpeech(context, (OnInitListener)this);
    } 
 
    public void queueSpeak(String textToSpeak) {
        if (mTts != null && isTTSInitialized) {
            mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, ttsParams);
            while (mTts.isSpeaking()) {
                try { 
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace();
                } 
            } 
        } else { 
7
CodeRank
This code example shows how to use the following methods:setLanguage
 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
 
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				myTTS = new TextToSpeech(this, this);
				myTTS.setLanguage(Locale.US);
			} else { 
				// TTS data not yet loaded, try to install it 
				Intent ttsLoadIntent = new Intent();
				ttsLoadIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(ttsLoadIntent);
			} 
		} 
	} 
 
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			 
			int n = (int)(Math.random() * phrases.size());
7
CodeRank
This code example shows how to use the following methods:setLanguage, setPitch, setSpeechRate, speak
    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.UK);
            // 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. 
                mTts.setPitch(1.6f);
                mTts.setSpeechRate(1.05f);

7
CodeRank
This code example shows how to use the following methods:setLanguage
 
    // Implements TextToSpeech.OnInitListener. 
    public void onInit(int status) {
        // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR. 
        if (status == TextToSpeech.SUCCESS) {
            int result = mTts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
               // Language data is missing or the language is not supported. 
                Log.e(TAG, "Language is not available.");
            } else { 
                Log.i(TAG, "TextToSpeech Initialized.");
                mSpeakingEngineAvailable = true;
            } 
        } else { 
            // Initialization failed. 
            Log.e(TAG, "Could not initialize TextToSpeech.");
        } 
    } 
 
6
CodeRank
This code example shows how to use the following methods:setOnUtteranceProgressListener
 
    private void setTtsUtteranceProgressListener() { 
        if (mTts == null) {
            return; 
        } 
        mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override 
            public void onStart(String utteranceId) {}
 
            @Override 
            public void onDone(String utteranceId) {}
 
            @Override 
            public void onError(String utteranceId) {
                Log.e(TAG, "Error while trying to synthesize sample text");
            } 
        }); 
    } 
 
    @Override 

6
CodeRank
This code example shows how to use the following methods:setLanguage, setOnUtteranceCompletedListener, shutdown, stop
    } 
     
    @Override 
    public void onDestroy() { 
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
        } 
        super.onDestroy(); 
    } 
 
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = mTts.setLanguage(Locale.ENGLISH);
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
            	this.onDestroy();
            mTts.setOnUtteranceCompletedListener(this);
        } 
    } 
 

6
CodeRank
This code example shows how to use the following methods:isLanguageAvailable, setLanguage, setPitch, setSpeechRate
    } 
     
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            Locale locale = Locale.ENGLISH;
            if (tts.isLanguageAvailable(locale) >=
                TextToSpeech.LANG_AVAILABLE) {
                tts.setLanguage(locale);
                tts.setPitch(1.5f);
                tts.setSpeechRate(1.5f);
                return; 
            } 
        } 
        Logger.d("Failed to initialize"); 
    } 
 
} 
6
CodeRank
This code example shows how to use the following methods:addEarcon, playEarcon
        if (earconName == null) {
            // We do not know the sound id, hence we need to load the sound. 
            Integer resourceId = sSoundsResourceIds.get(earconId);
            if (resourceId != null) {
                earconName = "[" + earconId + "]";
                mTts.addEarcon(earconName, getPackageName(), resourceId);
                mEarconNames.put(earconId, earconName);
            } 
        } 
 
        mTts.playEarcon(earconName, QUEUING_MODE_INTERRUPT, null);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:isLanguageAvailable, setLanguage, setOnUtteranceCompletedListener, setSpeechRate, sleep
 
	@SuppressWarnings("deprecation") 
	@Override 
	public void onInit(int arg0)
	{ 
		mTts.setOnUtteranceCompletedListener(this);
 
		Locale l = Locale.getDefault();
		int result = mTts.isLanguageAvailable(l);
		if (result == TextToSpeech.LANG_AVAILABLE || result == TextToSpeech.LANG_COUNTRY_AVAILABLE
				|| result == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE)
		{ 
 
			mTts.setLanguage(l);
		} else 
		{ 
			Toast.makeText(context, "Lang not available:" + result, Toast.LENGTH_LONG).show();
		} 
 
		mTts.setSpeechRate(0.85f);

6
CodeRank

Related Android TextToSpeech Questions & Answers:

Other Android TextToSpeech Examples

14:		/** Called when the activity is first created. */
15:	
16:		private TextToSpeech tts;
17:		private Button btnSpeak;
18:		private EditText txtText;
-
23:			setContentView(R.layout.main);
24:	
25:			tts = new TextToSpeech(this, this);
26:	
27:			btnSpeak = (Button) findViewById(R.id.btnSpeak);

Read More

See Code Examples for Android TextToSpeech Methods: