how can i use japanese google tts engine in my android app without change any device setting

☆樱花仙子☆ 提交于 2019-12-08 18:38:28

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

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!