印尼文字转语音

时间:2020-01-27 03:14:40

标签: android

我对印尼文字到语音有疑问,这是我的代码

public class ButtonListener {

private final TextToSpeech mTextToSpeech;
private final DictionaryFragment mDictionaryFragment;
private final DictionaryTypeState mDictionaryTypeState;

@BindView(R.id.search_words)
EditText mEditText;

public ButtonListener(DictionaryFragment dictionary, View root) {
    ButterKnife.bind(this, root);
    this.mDictionaryFragment = dictionary;
    this.mTextToSpeech = new TextToSpeech(dictionary.getContext(), null);
    this.mDictionaryTypeState = new DictionaryTypeState(dictionary.getContext());


}

@OnClick(R.id.clear_text)
void clearText() {
    mEditText.setText(ConstanValues.EMPTY);
    mTextToSpeech.stop();
}

@OnClick(R.id.speech_to_text)
void voiceInput() {
    openVoiceInput();
}

@OnClick(R.id.text_to_speech)
void speakText() {
    mTextToSpeech.setLanguage(getLangue());
    mTextToSpeech.speak(mEditText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
}

private final Locale getLangue() {

    return mDictionaryTypeState.getDictionarySourceState().equals(ConstanValues.ENGLISH_SOURCE)
            ? Locale.ENGLISH : new Locale("id", "ID");
}

private final void openVoiceInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
            .putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
            .putExtra(RecognizerIntent.EXTRA_LANGUAGE, new Locale("id", "ID"))
            .putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word");
    mDictionaryFragment.startActivityForResult(intent, ConstanValues.REQUEST_CODE);
}

public TextToSpeech getTextToSpeech() {
    return mTextToSpeech;
}

}

我制作了一个字典应用程序,可以使用英语和印尼文字进行语音转换 ,我为印度尼西亚语编写了一个代码,但是模拟器中始终显示的是英语,我的代码中是否缺少某些内容?我请求您的帮助

3 个答案:

答案 0 :(得分:2)

您需要使用该语言将文本初始化为语音

这是您的操作方式。

     textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
     @Override
     public void onInit(int status) {
         switch (status) {
             case TextToSpeech.SUCCESS: {
                 synchronized (this) {
                         int result = textToSpeech.setLanguage(<set the indonesia id here>);
                  <Take action based on the result of initialisation>

                 }
             }
             break;
             default: {
                 Toast.makeText(appContext, appContext.getResources().getString(R.string.tts_init_failed), Toast.LENGTH_LONG).show();
             }
             break;
         }
     }
 });

答案 1 :(得分:2)

通过更改代码中语言的方法尝试使用此代码

 Locale locale = new Locale("id");
    Locale.setDefault(locale);
    Configuration config = getBaseContext().getResources().getConfiguration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());

您可以在this链接中找到区域设置线

答案 2 :(得分:1)

创建一个类LocaleHelper

public class LocaleHelper {

private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

public static Context onAttach(Context context) {
    String lang = getPersistedData(context, Locale.getDefault().getLanguage());
    return setLocale(context, lang);
}

public static Context onAttach(Context context, String defaultLanguage) {
    String lang = getPersistedData(context, defaultLanguage);
    return setLocale(context, lang);
}

public static String getLanguage(Context context) {
    return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static Context setLocale(Context context, String language) {
    persist(context, language);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResources(context, language);
    }

    return updateResourcesLegacy(context, language);
}

private static String getPersistedData(Context context, String defaultLanguage) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static void persist(Context context, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString(SELECTED_LANGUAGE, language);
    editor.apply();
}

@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);

    return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());

    return context;
}

}

然后更改应用程序级别的语言环境

LocaleHelper.setLocale(YourActivity.this, "id");

之后,重新创建活动

private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);

}

从API 11(Honeycomb)开始,您应该使用recreate()而不是restartActivity()
restart