如何在不更改任何设备设置的情况下在我的 android 应用程序中使用日语谷歌 tts 引擎 [英] how can i use japanese google tts engine in my android app without change any device setting
问题描述
我正在开发一个日语学习应用.我想在我的应用中听到日语发音.
I am developing a Japanese study app. i wanna hear japanese pronunciation in my app.
我像这样初始化tts
private TextToSpeech tts;
private void initTTS()
{
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener()
{
@Override
public void onInit(int status)
{
if(status == TextToSpeech.SUCCESS)
{
Locale loc = new Locale("ja_JP");
int result = tts.setLanguage(loc);
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//installIntent.setPackage("com.google.android.tts");
startActivityForResult(installIntent,MY_DATA_CHECK_CODE);
}
else
{
Log.e(TAG,"initilization success");
}
}
else
{
Log.e(TAG,"initilization failed");
}
}
});
}
我手机的默认语言设置不是日语.我的应用用户都没有.
my phone's default language setting is not japanese. and my app users neither.
当默认语言设置不是日语时,tts.setLanguage 返回 TextToSpeech.LANG_MISSING_DATA.所以我用新活动安装了 tts 数据.但是谷歌tts引擎日语已经安装了.如何在不更改客户电话语言的情况下向我的客户提供日语 tts 服务.
when default laguage setting is not japanese, tts.setLanguage return TextToSpeech.LANG_MISSING_DATA. so i installed tts data with new activity. but google tts engine japanese already had been installed. how can i provide japanese tts service to my client without change client's phone language.
推荐答案
要初始化一个专门使用 google 引擎的 TTS 对象,而不管用户的首选引擎设置:
To initialize a TTS object that specifically uses the google engine regardless of the user's preferred engine settings:
private void createGoogleTTS() {
googleTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
Log.i("XXX", "Google tts initialized");
onTTSInitialized();
} else {
Log.i("XXX", "Internal Google engine init error.");
}
}
}, "com.google.android.tts");
}
当然,这只有在安装了谷歌引擎的情况下才有效,所以你也可以使用这些方法:
Of course, this will only work if the google engine is installed, so you could also use these methods:
private boolean isGoogleTTSInstalled() {
Intent ttsIntent = new Intent();
ttsIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
PackageManager pm = this.getPackageManager();
List<ResolveInfo> listOfInstalledTTSInfo = pm.queryIntentActivities(ttsIntent, PackageManager.GET_META_DATA);
for (ResolveInfo r : listOfInstalledTTSInfo) {
String engineName = r.activityInfo.applicationInfo.packageName;
if (engineName.equals("com.google.android.tts")) {
return true;
}
}
return false;
}
private void installGoogleTTS() {
Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("market://details?id=com.google.android.tts"));
startActivity(goToMarket);
}
// use this if attempting to speak in Japanese locale results in onError() being called by your UtteranceProgressListener.
private void openTTSSettingsToInstallUnsupportedLanguage() {
Intent intent = new Intent();
intent.setAction("com.android.settings.TTS_SETTINGS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
这篇关于如何在不更改任何设备设置的情况下在我的 android 应用程序中使用日语谷歌 tts 引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!