我有英文/法文申请。定义EXTRA_LANGUAGE_PREFERENCE是不够的。只要我更改系统语言输入以映射目标所需语言,我的应用程序就能正常运行。
知道我做错了什么吗?以下是我正在使用的代码。
private void initSpeech(){
Context context;
context = getActivity().getApplicationContext();
speech = SpeechRecognizer.createSpeechRecognizer(context);
mListener = new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
String s = null;
int ifl = 0;
ArrayList<String> thingsYouSaid = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
spoken.setVisibility(View.VISIBLE);
assert thingsYouSaid != null;
for(int i = 0; i<thingsYouSaid.size(); i++) {
if(thingsYouSaid.get(i).equals(words[curr_pos])) {
((TextView) getActivity().findViewById(R.id.dict_spoken_word)).setText(thingsYouSaid.get(i));
ifl = 1;
s = thingsYouSaid.get(i);
break;
}
}
if(ifl == 0) {
s = thingsYouSaid.get(0);
((TextView) getActivity().findViewById(R.id.dict_spoken_word)).setText(thingsYouSaid.get(0));
}
excellent.setVisibility(View.VISIBLE);
if (lang == 0) {
if (s.equals(words[curr_pos])) {
excellent.setTextColor(Color.GREEN);
excellent.setText(R.string.dict_resp);
count = 0;
return;
}
count++;
excellent.setTextColor(Color.BLUE);
if (count == 1) {
excellent.setText(R.string.dict_resp1);
} else if (count == 2)
excellent.setText(R.string.dict_resp2);
else {
excellent.setTextColor(Color.GREEN);
excellent.setText(R.string.dict_resp3);
count = 0;
}
}
else {
if (s.equals(words[curr_pos])) {
excellent.setTextColor(Color.GREEN);
excellent.setText(R.string.dict_resp_fr);
count = 0;
return;
}
count++;
excellent.setTextColor(Color.BLUE);
if (count == 1) {
excellent.setText(R.string.dict_resp1_fr);
} else if (count == 2)
excellent.setText(R.string.dict_resp2_fr);
else {
excellent.setTextColor(Color.GREEN);
excellent.setText(R.string.dict_resp3_fr);
count = 0;
}
}
if(speech_started) speech.stopListening();
speech_started = false;
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
};
speech.setRecognitionListener(mListener);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
if(lang == 0)
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en-US");
else
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "fr");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
getActivity().getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
}
private void speakDict(){
ttobj.speak(words[curr_pos], TextToSpeech.QUEUE_FLUSH, null);
}
private void talk2Me(){
new CountDownTimer(600, 100) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// if(speech_started) speech.stopListening();
speech.startListening(recognizerIntent);
}
}.start();
}
private void waitForResult(){
new CountDownTimer(600, 100) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
}.start();
}
答案 0 :(得分:2)
为了在不改变语言环境的情况下更改语言,我使用了here所描述的技术。我能够获得一个测试应用程序用法语转录,而我的语言环境是美国:
private void initSpeech(){
...
String myLanguage;
if (!isFrench) {
myLanguage = "en-US";
} else {
myLanguage = "fr";
}
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, myLanguage);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, myLanguage);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, myLanguage);
...
}
您需要确保在语言发生变化时随时拨打initSpeech()
,或者意图仍然会使用旧的语言标记。