我正在从事语音识别工作,我需要用多种语言来做这件事。
我到底想要什么,如果用户在印地语或任何其他语言说话,然后需要在文本视图上显示它。现在,它完美的工作在英语上。
根据用户的选择,我需要为多种语言执行此操作..
请帮帮我,谢谢..
以下是我的代码
public class MainActivity extends AppCompatActivity {
private static final int REQ_CODE_SPEECH_INPUT = 100;
private TextView mVoiceInputTv;
private ImageButton mSpeakBtn;
private TextToSpeech t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVoiceInputTv = (TextView) findViewById(R.id.voiceInput);
mSpeakBtn = (ImageButton) findViewById(R.id.btnSpeak);
mSpeakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startVoiceInput();
}
});
}
private void startVoiceInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help you?");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
}
catch (ActivityNotFoundException a) {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(userlanguage.equals("English")){
mVoiceInputTv.setText(result.get(0));
}
else if(userlanguage.equals("Hindi")){
face = Typeface.createFromAsset(getAssets(),"fonts/DroidHindi.ttf");
mVoiceInputTv.setTypeface(face);
mVoiceInputTv.setText(result.get(0));
}
else if(userlanguage.equals("Marathi")){
face = Typeface.createFromAsset(getAssets(),"fonts/Marathi.ttf");
mVoiceInputTv.setTypeface(face);
mVoiceInputTv.setText(result.get(0));
}
else if(userlanguage.equals("Gujarati")){
face = Typeface.createFromAsset(getAssets(),"fonts/Gujarati.ttf");
mVoiceInputTv.setTypeface(face);
mVoiceInputTv.setText(result.get(0));
}
}
break;
}
}
}
}发布于 2017-09-13 18:20:44
步骤1.您需要根据language selection intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.CHINESE);更改语言环境
步骤2.将语音文本保存到字符串文件中。并使用getString方法。这样你就可以根据语言来使用字符串了。

此代码可用于从不同的值文件夹中获取不同的语言字符串。
Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("pl");
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(getAssets(), metrics, conf);
String str = resources.getString(id);发布于 2017-09-13 18:21:51
谷歌的服务器目前支持英语、普通话和日语。web搜索模型在所有三种语言中都可用,而自由格式主要针对英语进行了优化。
设置EXTRA_LANGUAGE的值。请参阅http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_LANGUAGE
发布于 2019-04-27 11:25:17
您将为不同的语言创建不同的活动...
例如:假设您选择了印地语,现在它将转到印地语活动。就像这样……
代码:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");另一项活动:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "hi");我希望这能行得通。
https://stackoverflow.com/questions/46194774
复制相似问题