I am developing a Japanese study app. i wanna hear japanese pronunciation in my app.
i initialize tts like this
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.
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.
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);
}
来源:https://stackoverflow.com/questions/53910128/how-can-i-use-japanese-google-tts-engine-in-my-android-app-without-change-any-de