Android将语音转换为日语的语言不起作用

时间:2016-05-25 03:42:49

标签: android speech-recognition speech-to-text

您好我正在为Android制作日语学习应用程序。其中一个功能是用日语与应用程序对话,以检查您是否正确地说出了单词。我让它与promptSpeechInput一起工作,但我不喜欢ui阻碍我这样做,所以我决定再去一次溃败并让我的片段实现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;
}

}

2 个答案:

答案 0 :(得分:4)

试试这个

 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
}

答案 1 :(得分:2)

我研究了这个意图并让它运转起来。我用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");