2016-05-08 4 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

関連する問題