我正在尝试添加到我的应用程序特定的TTS引擎 - 不是基于系统的,所以每个人都会有另一个,但一个人。
在文档中有方法:setEngineByPackageName(),看起来它会成为我想要的东西。但是,在查看其他类似问题之前,我发现了一些使用此方法的内容:https://stackoverflow.com/questions/12549086/selecting-required-tts-programmatically-in-android。
它看起来很不错,但在系统检查后是否安装了TTS引擎,如果没有安装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商店链接。
答案 0 :(得分:1)
为什么在onCreate上调用意图?
将安装检查例程移到说话之前的位置
答案 1 :(得分:0)
尝试在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;
}
}