I am trying to add to my app specific TTS engine - not system based, so each person will have another, but one for all.
In documentation there is method:setEngineByPackageName(), which looks like it will make what I want. However, looking on other similar problems earlier I've found something using this method: https://stackoverflow.com/questions/12549086/selecting-required-tts-programmatically-in-android.
It looks quite ok, but it is used after system checking if TTS engine is installed, and installing it if there isn't one (no defined which one).
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Fire off an intent to check if a TTS engine is installed
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == MY_DATA_CHECK_CODE)
{
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
{
// success, create the TTS instance
mTts = new TextToSpeech(this, this);
}
else
{
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
So, I would like to ask if there is any method to install specific tts engine during checking, because now those methods are called before creating TTS, so you can't call setEngineByPackageName(), or constructor with engine setting (depends on Android version).
I was thinking as an engine about Text-To-Speech Extended, so as I understand I should use package name: com.google.tts I assume it from Play store link.
try it in onCreate() for check specific TTS engine is installed or not:
if ((isPackageInstalled(getPackageManager(), SPECIFIC_TTS_PACKAGE_NAME))) {
Log.e(TTS_TAG, "Intended TTS engine installed");
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.ERROR) {
Log.e(TTS_TAG, "TTS initialize failed");
} else {
int result = mTTS.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_NOT_SUPPORTED
|| result == TextToSpeech.LANG_MISSING_DATA) {
Log.e(TTS_TAG, "Language not supported");
} else {
mButtonSpeak.setEnabled(true);
}
}
}
}, SPECIFIC_TTS_PACKAGE_NAME);
} else {
Log.e(TTS_TAG, "Intended TTS engine is not installed");
installSpecificTTSEngine();
}
and this method for install TTS engine:
private void installSpecificTTSEngine() {
if (internetIsConnected()) {
Intent installIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+SPECIFIC_TTS_PACKAGE_NAME));
installIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
try {
Log.e(TTS_TAG, "Installing TTS engine: " + installIntent.toUri(0));
startActivity(installIntent);
} catch (ActivityNotFoundException ex) {
Log.e(TTS_TAG, "Failed to install TTS engine, no activity found for " + installIntent + ")");
}
} else {
Log.e(TTS_TAG, "Internet is not connected for download tts engine");
}
}
other methods:
public static boolean isPackageInstalled(PackageManager pm, String packageName) {
try {
pm.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
/* Check is there a NetworkConnection */
protected boolean internetIsConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm != null ? cm.getActiveNetworkInfo() : null;
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
return false;
}
}
why you invoke intent at onCreate?
move installation checking routine to where just before speak
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With