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");
}
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");
}
}
}
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;
}
});
}
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;
}
});
}
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);
}
Aggregations