我正在尝试将特定的TTS引擎添加到我的应用程序中-不是基于系统的,因此每个人都会有另一个,但所有人都可以。
在文档中有方法:setEngineByPackageName(),它看起来像我想要的那样。但是,在更早地研究其他类似问题时,我发现了使用此方法的一些东西:https://stackoverflow.com/questions/12549086/selecting-required-tts-programmatically-in-android。
看起来还不错,但是在系统检查是否安装了TTS引擎之后再使用它,如果没有安装(没有定义哪个),则在安装它后使用。
@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版本) )。
我当时想作为文本语音转换扩展的引擎,所以据我了解,我应该使用包名称:com.google.tts,我认为它来自Play商店链接。
最佳答案
尝试在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;
}
}
关于android - 如何在Android中以编程方式安装特定的TTS引擎?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15052999/