我正致力于语音识别,我需要使用多种语言。
如果用户用印地语或任何其他语言说话,我真正想要的是,然后需要在文本View上显示它。 现在它完全适合英语。
根据用户的选择,我需要为多种语言做些什么..请帮帮我,谢谢..
这是我的代码
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;
}
}
}
}
答案 0 :(得分:0)
步骤1.您需要根据语言选择更改您的语言环境 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);
答案 1 :(得分:0)
Google的服务器目前支持英语,普通话和日语。网络搜索模型以所有三种语言提供,而自由形式主要针对英语进行了优化。
设置EXTRA_LANGUAGE的值。见http://developer.android.com/reference/android/speech/RecognizerIntent.html#EXTRA_LANGUAGE
答案 2 :(得分:0)
您将为不同的语言创建不同的活动...。
例如:假设您选择了印地语语言,现在它将进入“印地语语言活动..”。
代码:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
另一项活动:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "hi");
我希望这会起作用...