user2575373 user2575373 - 6 months ago 27
Android Question

Android Change language of speech to text to Japanese not working

Hi I'm making a Japanese learning app for android. One of the features is to speak to the app in Japanese to check if you are saying words correctly. I got it working with

promptSpeechInput
but I did not like the
ui
getting in the way so I decided to go another rout and have my Fragment implement
RecognitionListener
. For some reason now Japanese is not working and it shows English words. I'm not sure what's wrong.

Here is my code for my Speech 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;
}


}

Answer

Try like this

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

After that override the onActivityResult() method in your activity file(where you called the 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
}
Comments