我有印尼文本到语音的问题,这是我的代码
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;
}
}我做了一个字典应用程序,可以使用英语和印尼语文本进行语音转换,我为印尼语编写了代码,但模拟器中显示的总是英语,我的代码中是否缺少什么?我恳求你的帮助
发布于 2020-01-27 13:28:51
您需要使用该语言将文本初始化为语音
下面是你如何做到这一点。
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;
}
}
});发布于 2020-01-27 13:46:22
尝试使用更改代码中的语言的方法来执行此代码
Locale locale = new Locale("id");
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());您可以在this链接中找到区域设置数据线
https://stackoverflow.com/questions/59924796
复制相似问题