如何在 Android 中以编程方式安装特定的 TTS 引擎? [英] How to install specific TTS engine programmatically in Android?

查看:84
本文介绍了如何在 Android 中以编程方式安装特定的 TTS 引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将特定于我的应用的 TTS 引擎添加到我的应用程序中 - 不是基于系统的,因此每个人都会有另一个,但适用于所有人.

I am trying to add to my app specific TTS engine - not system based, so each person will have another, but one for all.

在文档中有方法:setEngineByPackageName(),这看起来像是我想要的.但是,在之前查看其他类似问题时,我发现了一些使用此方法的内容:https://stackoverflow.com/questions/12549086/selecting-required-tts-programmatically-in-android.

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.

看起来还可以,但是系统检查是否安装了TTS引擎后使用,如果没有安装(未定义是哪个).

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

所以,请问有没有什么方法可以在检查时安装特定的tts引擎,因为现在这些方法是在创建TTS之前调用的,所以不能调用setEngineByPackageName(),或者带有引擎设置的构造函数(取决于在安卓版本上).

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).

我想作为一个关于Text-To-Speech Extended的引擎,所以据我所知,我应该使用包名称:com.google.tts 我假设它来自 Play 商店链接.

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.

推荐答案

在 onCreate() 中试试看是否安装了特定的 TTS 引擎:

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

以及安装TTS引擎的方法:

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

其他方法:

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

这篇关于如何在 Android 中以编程方式安装特定的 TTS 引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆