Search in sources :

Example 41 with TextToSpeech

use of android.speech.tts.TextToSpeech in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class TtsEnginePreferenceFragment method updateDefaultEngine.

private void updateDefaultEngine(String engine) {
    Log.d(TAG, "Updating default synth to : " + engine);
    // Keep track of the previous engine that was being used. So that
    // we can reuse the previous engine.
    // 
    // Note that if TextToSpeech#getCurrentEngine is not null, it means at
    // the very least that we successfully bound to the engine service.
    mPreviousEngine = mTts.getCurrentEngine();
    // Step 1: Shut down the existing TTS engine.
    Log.i(TAG, "Shutting down current tts engine");
    if (mTts != null) {
        try {
            mTts.shutdown();
            mTts = null;
        } catch (Exception e) {
            Log.e(TAG, "Error shutting down TTS engine" + e);
        }
    }
    // Step 2: Connect to the new TTS engine.
    // Step 3 is continued on #onUpdateEngine (below) which is called when
    // the app binds successfully to the engine.
    Log.i(TAG, "Updating engine : Attempting to connect to engine: " + engine);
    mTts = new TextToSpeech(getActivity().getApplicationContext(), mUpdateListener, engine);
    Log.i(TAG, "Success");
}
Also used : TextToSpeech(android.speech.tts.TextToSpeech)

Example 42 with TextToSpeech

use of android.speech.tts.TextToSpeech in project android_packages_apps_Settings by LineageOS.

the class TextToSpeechSettings method onActivityResult.

/**
 * Called when voice data integrity check returns
 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_DATA_INTEGRITY_CHECK) {
        if (data == null) {
            // The CHECK_TTS_DATA activity for the plugin did not run properly;
            // disable the preview and install controls and return.
            mEnableDemo = false;
            mVoicesMissing = false;
            updateWidgetState();
            return;
        }
        ArrayList<String> available = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
        ArrayList<String> unavailable = data.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES);
        if ((available == null) || (unavailable == null)) {
            // The CHECK_TTS_DATA activity for the plugin did not run properly;
            // disable the preview and install controls and return.
            mEnableDemo = false;
            mVoicesMissing = false;
            updateWidgetState();
            return;
        }
        if (available.size() > 0) {
            if (mTts == null) {
                mTts = new TextToSpeech(this, this);
            }
            ListPreference ttsLanguagePref = (ListPreference) findPreference("tts_default_lang");
            CharSequence[] entries = new CharSequence[available.size()];
            CharSequence[] entryValues = new CharSequence[available.size()];
            int selectedLanguageIndex = -1;
            String selectedLanguagePref = mDefaultLanguage;
            if (mDefaultCountry.length() > 0) {
                selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER + mDefaultCountry;
            }
            if (mDefaultLocVariant.length() > 0) {
                selectedLanguagePref = selectedLanguagePref + LOCALE_DELIMITER + mDefaultLocVariant;
            }
            for (int i = 0; i < available.size(); i++) {
                String[] langCountryVariant = available.get(i).split("-");
                Locale loc = null;
                if (langCountryVariant.length == 1) {
                    loc = new Locale(langCountryVariant[0]);
                } else if (langCountryVariant.length == 2) {
                    loc = new Locale(langCountryVariant[0], langCountryVariant[1]);
                } else if (langCountryVariant.length == 3) {
                    loc = new Locale(langCountryVariant[0], langCountryVariant[1], langCountryVariant[2]);
                }
                if (loc != null) {
                    entries[i] = loc.getDisplayName();
                    entryValues[i] = available.get(i);
                    if (entryValues[i].equals(selectedLanguagePref)) {
                        selectedLanguageIndex = i;
                    }
                }
            }
            ttsLanguagePref.setEntries(entries);
            ttsLanguagePref.setEntryValues(entryValues);
            if (selectedLanguageIndex > -1) {
                ttsLanguagePref.setValueIndex(selectedLanguageIndex);
            }
            mEnableDemo = true;
            // Make sure that the default language can be used.
            int languageResult = mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
            if (languageResult < TextToSpeech.LANG_AVAILABLE) {
                Locale currentLocale = Locale.getDefault();
                mDefaultLanguage = currentLocale.getISO3Language();
                mDefaultCountry = currentLocale.getISO3Country();
                mDefaultLocVariant = currentLocale.getVariant();
                languageResult = mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
                // language so that there is at least something.
                if (languageResult < TextToSpeech.LANG_AVAILABLE) {
                    parseLocaleInfo(ttsLanguagePref.getEntryValues()[0].toString());
                    mTts.setLanguage(new Locale(mDefaultLanguage, mDefaultCountry, mDefaultLocVariant));
                }
                ContentResolver resolver = getContentResolver();
                Settings.Secure.putString(resolver, TTS_DEFAULT_LANG, mDefaultLanguage);
                Settings.Secure.putString(resolver, TTS_DEFAULT_COUNTRY, mDefaultCountry);
                Settings.Secure.putString(resolver, TTS_DEFAULT_VARIANT, mDefaultLocVariant);
            }
        } else {
            mEnableDemo = false;
        }
        if (unavailable.size() > 0) {
            mVoicesMissing = true;
        } else {
            mVoicesMissing = false;
        }
        updateWidgetState();
    } else if (requestCode == GET_SAMPLE_TEXT) {
        if (resultCode == TextToSpeech.LANG_AVAILABLE) {
            String sample = getString(R.string.tts_demo);
            if ((data != null) && (data.getStringExtra("sampleText") != null)) {
                sample = data.getStringExtra("sampleText");
            }
            if (mTts != null) {
                mTts.speak(sample, TextToSpeech.QUEUE_FLUSH, null);
            }
        } else {
            // TODO: Display an error here to the user.
            Log.e(TAG, "Did not have a sample string for the requested language");
        }
    }
}
Also used : Locale(java.util.Locale) ListPreference(android.preference.ListPreference) TextToSpeech(android.speech.tts.TextToSpeech) ContentResolver(android.content.ContentResolver)

Example 43 with TextToSpeech

use of android.speech.tts.TextToSpeech in project xDrip by NightscoutFoundation.

the class SpeechUtil method initialize.

// set up an instance to Android TTS with our desired language and settings
private static synchronized void initialize() {
    if (tts != null)
        return;
    tts = new TextToSpeech(xdrip.getAppContext(), status -> {
        if (status == TextToSpeech.SUCCESS && tts != null) {
            UserError.Log.d(TAG, "Initializing, successful result code: " + status);
            final Locale speech_locale = chosenLocale();
            UserError.Log.d(TAG, "Chosen locale: " + speech_locale);
            int set_language_result;
            // try setting the language we want
            try {
                set_language_result = tts.setLanguage(speech_locale);
            } catch (IllegalArgumentException e) {
                // can end up here with Locales like "OS"
                UserError.Log.e(TAG, "Got TTS set language error: " + e.toString());
                set_language_result = TextToSpeech.LANG_MISSING_DATA;
            } catch (Exception e) {
                // can end up here with deep errors from tts system
                UserError.Log.e(TAG, "Got TTS set language deep error: " + e.toString());
                set_language_result = TextToSpeech.LANG_MISSING_DATA;
            }
            // try various fallbacks
            if (set_language_result == TextToSpeech.LANG_MISSING_DATA || set_language_result == TextToSpeech.LANG_NOT_SUPPORTED) {
                UserError.Log.e(TAG, "Default system language is not supported");
                try {
                    set_language_result = tts.setLanguage(Locale.ENGLISH);
                } catch (IllegalArgumentException e) {
                    // can end up here with parcel Locales like "OS"
                    UserError.Log.e(TAG, "Got TTS set default language error: " + e.toString());
                    set_language_result = TextToSpeech.LANG_MISSING_DATA;
                } catch (Exception e) {
                    // can end up here with deep errors from tts system
                    UserError.Log.e(TAG, "Got TTS set default language deep error: " + e.toString());
                    set_language_result = TextToSpeech.LANG_MISSING_DATA;
                }
            }
            // try any english as last resort
            if (set_language_result == TextToSpeech.LANG_MISSING_DATA || set_language_result == TextToSpeech.LANG_NOT_SUPPORTED) {
                UserError.Log.e(TAG, "English is not supported! total failure");
                tts = null;
            }
        } else {
            UserError.Log.e(TAG, "Initialize status code indicates failure, code: " + status);
            tts = null;
        }
    });
}
Also used : Context(android.content.Context) JoH(com.eveningoutpost.dexdrip.Models.JoH) PowerManager(android.os.PowerManager) TextToSpeech(android.speech.tts.TextToSpeech) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) ActivityNotFoundException(android.content.ActivityNotFoundException) Locale(java.util.Locale) Intent(android.content.Intent) UserError(com.eveningoutpost.dexdrip.Models.UserError) AudioManager(android.media.AudioManager) Locale(java.util.Locale) TextToSpeech(android.speech.tts.TextToSpeech) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 44 with TextToSpeech

use of android.speech.tts.TextToSpeech in project xDrip-plus by jamorham.

the class SpeechUtil method initialize.

// set up an instance to Android TTS with our desired language and settings
private static synchronized void initialize() {
    if (tts != null)
        return;
    tts = new TextToSpeech(xdrip.getAppContext(), status -> {
        if (status == TextToSpeech.SUCCESS && tts != null) {
            UserError.Log.d(TAG, "Initializing, successful result code: " + status);
            final Locale speech_locale = chosenLocale();
            UserError.Log.d(TAG, "Chosen locale: " + speech_locale);
            int set_language_result;
            // try setting the language we want
            try {
                set_language_result = tts.setLanguage(speech_locale);
            } catch (IllegalArgumentException e) {
                // can end up here with Locales like "OS"
                UserError.Log.e(TAG, "Got TTS set language error: " + e.toString());
                set_language_result = TextToSpeech.LANG_MISSING_DATA;
            } catch (Exception e) {
                // can end up here with deep errors from tts system
                UserError.Log.e(TAG, "Got TTS set language deep error: " + e.toString());
                set_language_result = TextToSpeech.LANG_MISSING_DATA;
            }
            // try various fallbacks
            if (set_language_result == TextToSpeech.LANG_MISSING_DATA || set_language_result == TextToSpeech.LANG_NOT_SUPPORTED) {
                UserError.Log.e(TAG, "Default system language is not supported");
                try {
                    set_language_result = tts.setLanguage(Locale.ENGLISH);
                } catch (IllegalArgumentException e) {
                    // can end up here with parcel Locales like "OS"
                    UserError.Log.e(TAG, "Got TTS set default language error: " + e.toString());
                    set_language_result = TextToSpeech.LANG_MISSING_DATA;
                } catch (Exception e) {
                    // can end up here with deep errors from tts system
                    UserError.Log.e(TAG, "Got TTS set default language deep error: " + e.toString());
                    set_language_result = TextToSpeech.LANG_MISSING_DATA;
                }
            }
            // try any english as last resort
            if (set_language_result == TextToSpeech.LANG_MISSING_DATA || set_language_result == TextToSpeech.LANG_NOT_SUPPORTED) {
                UserError.Log.e(TAG, "English is not supported! total failure");
                tts = null;
            }
        } else {
            UserError.Log.e(TAG, "Initialize status code indicates failure, code: " + status);
            tts = null;
        }
    });
}
Also used : Context(android.content.Context) JoH(com.eveningoutpost.dexdrip.Models.JoH) PowerManager(android.os.PowerManager) TextToSpeech(android.speech.tts.TextToSpeech) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) ActivityNotFoundException(android.content.ActivityNotFoundException) Locale(java.util.Locale) Intent(android.content.Intent) UserError(com.eveningoutpost.dexdrip.Models.UserError) AudioManager(android.media.AudioManager) Locale(java.util.Locale) TextToSpeech(android.speech.tts.TextToSpeech) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 45 with TextToSpeech

use of android.speech.tts.TextToSpeech in project android_packages_apps_Settings by omnirom.

the class TtsEnginePreferenceFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    mContext = getContext().getApplicationContext();
    mEnginesHelper = new TtsEngines(mContext);
    mEngineMap = new HashMap<>();
    mTts = new TextToSpeech(mContext, null);
    super.onCreate(savedInstanceState);
}
Also used : TtsEngines(android.speech.tts.TtsEngines) TextToSpeech(android.speech.tts.TextToSpeech)

Aggregations

TextToSpeech (android.speech.tts.TextToSpeech)103 Test (org.junit.Test)34 Locale (java.util.Locale)18 TtsEngines (android.speech.tts.TtsEngines)15 ActivityNotFoundException (android.content.ActivityNotFoundException)12 Intent (android.content.Intent)12 ContentResolver (android.content.ContentResolver)10 MissingResourceException (java.util.MissingResourceException)10 IntentFilter (android.content.IntentFilter)8 TextView (android.widget.TextView)7 Config (org.robolectric.annotation.Config)7 BluetoothAdapter (android.bluetooth.BluetoothAdapter)6 Handler (android.os.Handler)6 UtteranceProgressListener (android.speech.tts.UtteranceProgressListener)6 ArrayAdapter (android.widget.ArrayAdapter)6 Context (android.content.Context)5 HashMap (java.util.HashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Bundle (android.os.Bundle)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3