簡體   English   中英

Android將語音語言從文本更改為日語無法正常工作

[英]Android Change language of speech to text to Japanese not working

嗨,我正在為Android開發日語學習應用程序。 其中一項功能是使用日語與該應用說話,以檢查您是否正確說出了單詞。 我在使用promptSpeechInput了它,但是我不喜歡ui妨礙用戶使用,因此我決定進行另一次嘗試,並讓我的Fragment實現RecognitionListener 由於某種原因,日語現在無法使用,並且顯示英語單詞。 我不知道怎么了。

這是我的語音片段代碼

public class SpeechFragment extends Fragment implements RecognitionListener {

private TextView textViewInput;
private ToggleButton buttonSpeak;
private SpeechRecognizer speech = null;
private Intent recognizerIntent;

public SpeechFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_speech, container, false);


    speech = SpeechRecognizer.createSpeechRecognizer(this.getContext());
    speech.setRecognitionListener(this);

    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    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.JAPANESE);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);



    textViewInput = (TextView) view.findViewById(R.id.textViewInput);
    buttonSpeak = (ToggleButton) view.findViewById(R.id.buttonSpeak);
    buttonSpeak.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            if (isChecked) {
                speech.startListening(recognizerIntent);
            } else {
                speech.stopListening();
            }
        }
    });

return view;

}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
    if (speech != null) {
        speech.destroy();
    }

}

@Override
public void onBeginningOfSpeech() {
    textViewInput.setText("Speak");
}

@Override
public void onBufferReceived(byte[] buffer) {
      }

@Override
public void onEndOfSpeech() {
    buttonSpeak.setChecked(false);
}

@Override
public void onError(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    textViewInput.setText(errorMessage);
    buttonSpeak.setChecked(false);
}

@Override
public void onEvent(int arg0, Bundle arg1) {
}

@Override
public void onPartialResults(Bundle arg0) {
}

@Override
public void onReadyForSpeech(Bundle arg0) {
}

@Override
public void onResults(Bundle results) {
    ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    for (String result : matches)
        text += result + "\n";

    textViewInput.setText(text);
}

@Override
public void onRmsChanged(float rmsdB) {
}

public static String getErrorText(int errorCode) {
    String message;
    switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
    }
    return message;
}

}

這樣嘗試

 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.JAPANESE);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
getActivity().startActivityForResult(intent,requestCode);

之后,在您的活動文件(您在其中調用了片段)中覆蓋onActivityResult()方法。

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    ArrayList<String> words=data.getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
    //Here you can get the spoken words
}

我致力於這個意圖,並使它起作用。 我用ja_JP替換了Local.JAPANESE並成功了。

    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ja_JP");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speak");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM