Search in sources :

Example 91 with TextToSpeech

use of android.speech.tts.TextToSpeech in project platform_packages_apps_Settings by BlissRoms.

the class TtsEnginePreferenceFragment method onUpdateEngine.

/**
 * Step 3: We have now bound to the TTS engine the user requested. We will attempt to check
 * voice data for the engine if we successfully bound to it, or revert to the previous engine if
 * we didn't.
 */
public void onUpdateEngine(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Log.d(TAG, "Updating engine: Successfully bound to the engine: " + mTts.getCurrentEngine());
        android.provider.Settings.Secure.putString(getContentResolver(), TTS_DEFAULT_SYNTH, mTts.getCurrentEngine());
    } else {
        Log.d(TAG, "Updating engine: Failed to bind to engine, reverting.");
        if (mPreviousEngine != null) {
            // This is guaranteed to at least bind, since mPreviousEngine would be
            // null if the previous bind to this engine failed.
            mTts = new TextToSpeech(getActivity().getApplicationContext(), null, mPreviousEngine);
        }
        mPreviousEngine = null;
    }
}
Also used : TextToSpeech(android.speech.tts.TextToSpeech)

Example 92 with TextToSpeech

use of android.speech.tts.TextToSpeech in project Anki-Android by Ramblurr.

the class ReadText method initializeTts.

public static void initializeTts(Context context) {
    mReviewer = context;
    mTts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {
            // TODO: check if properly initialized (does not work yet)
            if (status != TextToSpeech.SUCCESS) {
                int result = mTts.setLanguage(Locale.US);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                } else {
                    Log.e(AnkiDroidApp.TAG, "TTS initialized and set to US");
                }
            } else {
                Log.e(AnkiDroidApp.TAG, "Initialization of TTS failed");
            }
            AnkiDroidApp.getCompat().setTtsOnUtteranceProgressListener(mTts);
        }
    });
    mTtsParams = new HashMap<String, String>();
    mTtsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "stringId");
}
Also used : TextToSpeech(android.speech.tts.TextToSpeech)

Example 93 with TextToSpeech

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

the class TextToSpeechSettings method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.tts_settings);
    getActivity().setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
    mEnginesHelper = new TtsEngines(getActivity().getApplicationContext());
    mLocalePreference = (ListPreference) findPreference(KEY_ENGINE_LOCALE);
    mLocalePreference.setOnPreferenceChangeListener(this);
    mDefaultPitchPref = (SeekBarPreference) findPreference(KEY_DEFAULT_PITCH);
    mDefaultRatePref = (SeekBarPreference) findPreference(KEY_DEFAULT_RATE);
    mActionButtons = ((ActionButtonsPreference) findPreference(KEY_ACTION_BUTTONS)).setButton1Text(R.string.tts_play).setButton1OnClickListener(v -> speakSampleText()).setButton1Enabled(false).setButton2Text(R.string.tts_reset).setButton2OnClickListener(v -> resetTts()).setButton1Enabled(true);
    if (savedInstanceState == null) {
        mLocalePreference.setEnabled(false);
        mLocalePreference.setEntries(new CharSequence[0]);
        mLocalePreference.setEntryValues(new CharSequence[0]);
    } else {
        // Repopulate mLocalePreference with saved state. Will be updated later with
        // up-to-date values when checkTtsData() calls back with results.
        final CharSequence[] entries = savedInstanceState.getCharSequenceArray(STATE_KEY_LOCALE_ENTRIES);
        final CharSequence[] entryValues = savedInstanceState.getCharSequenceArray(STATE_KEY_LOCALE_ENTRY_VALUES);
        final CharSequence value = savedInstanceState.getCharSequence(STATE_KEY_LOCALE_VALUE);
        mLocalePreference.setEntries(entries);
        mLocalePreference.setEntryValues(entryValues);
        mLocalePreference.setValue(value != null ? value.toString() : null);
        mLocalePreference.setEnabled(entries.length > 0);
    }
    mTts = new TextToSpeech(getActivity().getApplicationContext(), mInitListener);
    setTtsUtteranceProgressListener();
    initSettings();
    // Prevent restarting the TTS connection on rotation
    setRetainInstance(true);
}
Also used : SettingsPreferenceFragment(com.android.settings.SettingsPreferenceFragment) Context(android.content.Context) Arrays(java.util.Arrays) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) SeekBarPreference(com.android.settings.widget.SeekBarPreference) Pair(android.util.Pair) UtteranceProgressListener(android.speech.tts.UtteranceProgressListener) Intent(android.content.Intent) HashMap(java.util.HashMap) SearchIndexable(com.android.settingslib.search.SearchIndexable) ArrayList(java.util.ArrayList) Indexable(com.android.settings.search.Indexable) SearchIndexableResource(android.provider.SearchIndexableResource) TtsEngines(android.speech.tts.TtsEngines) ContentResolver(android.content.ContentResolver) EngineInfo(android.speech.tts.TextToSpeech.EngineInfo) Locale(java.util.Locale) TTS_DEFAULT_RATE(android.provider.Settings.Secure.TTS_DEFAULT_RATE) Log(android.util.Log) R(com.android.settings.R) BaseSearchIndexProvider(com.android.settings.search.BaseSearchIndexProvider) TTS_DEFAULT_PITCH(android.provider.Settings.Secure.TTS_DEFAULT_PITCH) TTS_DEFAULT_SYNTH(android.provider.Settings.Secure.TTS_DEFAULT_SYNTH) MissingResourceException(java.util.MissingResourceException) Set(java.util.Set) SettingsEnums(android.app.settings.SettingsEnums) TextUtils(android.text.TextUtils) GearPreference(com.android.settings.widget.GearPreference) ActionButtonsPreference(com.android.settingslib.widget.ActionButtonsPreference) Preference(androidx.preference.Preference) Objects(java.util.Objects) TextToSpeech(android.speech.tts.TextToSpeech) List(java.util.List) SettingsActivity(com.android.settings.SettingsActivity) ActivityNotFoundException(android.content.ActivityNotFoundException) ListPreference(androidx.preference.ListPreference) Collections(java.util.Collections) ActionButtonsPreference(com.android.settingslib.widget.ActionButtonsPreference) TtsEngines(android.speech.tts.TtsEngines) TextToSpeech(android.speech.tts.TextToSpeech)

Example 94 with TextToSpeech

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

the class TextToSpeechSettings method onResume.

@Override
public void onResume() {
    super.onResume();
    // We tend to change the summary contents of our widgets, which at higher text sizes causes
    // them to resize, which results in the recyclerview smoothly animating them at inopportune
    // times. Disable the animation so widgets snap to their positions rather than sliding
    // around while the user is interacting with it.
    getListView().getItemAnimator().setMoveDuration(0);
    if (mTts == null || mCurrentDefaultLocale == null) {
        return;
    }
    if (!mTts.getDefaultEngine().equals(mTts.getCurrentEngine())) {
        try {
            mTts.shutdown();
            mTts = null;
        } catch (Exception e) {
            Log.e(TAG, "Error shutting down TTS engine" + e);
        }
        mTts = new TextToSpeech(getActivity().getApplicationContext(), mInitListener);
        setTtsUtteranceProgressListener();
        initSettings();
    } else {
        // Do set pitch correctly after it may have changed, and unlike speed, it doesn't change
        // immediately.
        final ContentResolver resolver = getContentResolver();
        mTts.setPitch(android.provider.Settings.Secure.getInt(resolver, TTS_DEFAULT_PITCH, TextToSpeech.Engine.DEFAULT_PITCH) / 100.0f);
    }
    Locale ttsDefaultLocale = mTts.getDefaultLanguage();
    if (mCurrentDefaultLocale != null && !mCurrentDefaultLocale.equals(ttsDefaultLocale)) {
        updateWidgetState(false);
        checkDefaultLocale();
    }
}
Also used : Locale(java.util.Locale) TextToSpeech(android.speech.tts.TextToSpeech) MissingResourceException(java.util.MissingResourceException) ActivityNotFoundException(android.content.ActivityNotFoundException) ContentResolver(android.content.ContentResolver)

Example 95 with TextToSpeech

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

the class TtsEnginePreferenceFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.tts_engine_picker);
    mEnginePreferenceCategory = (PreferenceCategory) findPreference("tts_engine_preference_category");
    mEnginesHelper = new TtsEngines(getActivity().getApplicationContext());
    mTts = new TextToSpeech(getActivity().getApplicationContext(), null);
    initSettings();
}
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