2016-05-08 70 views
1

我有英語/法語應用程序。定義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

以編程方式設置當前語言環境。 –

+1

你的帖子中包含了許多不相關的代碼。包括一個簡單的示例,其中包含足夠的代碼來重現您的問題很有幫助 - 請參閱[this](http://stackoverflow.com/help/mcve)。 – Michiyo

回答

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()任何時候語言改變或意圖仍然有舊的語言標誌。

+0

它效果更好!謝謝。 – narb