我正在开发一个语音识别系统,我用英语制作了一个,并且效果非常好。
但我遇到的问题是我需要开发应用程序来识别阿拉伯语。
这是我的代码:
private static final int RESULT_SPEECH = 1;
Button b1 ;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent voicerecogize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "ar-eg");
startActivityForResult(voicerecogize, RESULT_SPEECH);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RESULT_SPEECH && requestCode == RESULT_OK);
{
ArrayList<String > results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , results));
}
super.onActivityResult(requestCode, resultCode, data);
}
答案 0 :(得分:7)
将onClick
事件的代码更改为:
Intent voicerecogize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voicerecogize.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, ecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-EG");
startActivityForResult(voicerecogize, RESULT_SPEECH);
答案 1 :(得分:2)
仅供参考,在Kitkat设备中,即使我指定的语言(阿拉伯语)不是默认语言(英语),它也会使用默认语言(英语)。
Issue 76347-https://code.google.com/p/android/issues/detail?id=75347
我从上面的链接找到了问题的解决方案,这只不过是用RecognizerIntent添加以下语句。
intent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{});
添加后,我的应用程序在Kitkat 4.4中正常工作