How to programmatically install a specific TTS engine in Android?

I’m trying to add a special TTS engine to my application - not based on the system, so each person will have a different one, but one for everyone.

There is a method in the documentation: setEngineByPackageName () , which looks like it will do what I want. However, looking at other similar problems earlier, I found something using this method: https://stackoverflow.com/questions/12549086/selecting-required-tts-programmatics-in-android .

It looks quite normal, but it is used after checking the system whether the TTS engine is installed and installing it if it is not (it is not determined 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);
        }
    }
}

, , - tts , TTS, setEngineByPackageName() ( Android).

Text-To-Speech Extended, , : com.google.tts , Play store.

+6
2

onCreate?

,

+1

onCreate(), , TTS:

        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();
    }

TTS:

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

:

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;
    }
}
0

All Articles