gpt4 book ai didi

Android 将语音语言更改为文本到日语不起作用

转载 作者:行者123 更新时间:2023-11-29 14:46:04 25 4
gpt4 key购买 nike

您好,我正在为安卓开发一个日语学习应用。其中一项功能是用日语与应用程序交谈,以检查您是否说对了单词。我用 promptSpeechInput 让它工作,但我不喜欢 ui 挡路,所以我决定另辟蹊径,让我的 Fragment 实现 RecognitionListener。由于某种原因,现在日语不起作用,它显示英文单词。我不确定出了什么问题。

这是我的语音 fragment 代码

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);

之后覆盖您的 Activity 文件中的 onActivityResult() 方法(您调用 fragment 的地方)

 @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
}

关于Android 将语音语言更改为文本到日语不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37427423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com