I'm using the SpeechRecognizer
to try to get some German strings back. I'm passing in a Google Translate German recording (e.g., "How are you?" -> "Wie geht es Ihnen?").
I've looked at several SO question/answers about how to change the language to accept something other than English but none seem to work.
Does anyone know how to do this while keeping the device's language setting to English (en-US)?
I'm able to successfully get German strings back only when I set the device's language to "de-DE".
Here's my sample code:
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
Button mSpeakBtn;
TextView mResultTextView;
SpeechRecognizer mSpeechRecognizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init Views
mSpeakBtn = (Button) findViewById(R.id.speak_btn);
mResultTextView = (TextView) findViewById(R.id.result_tv);
// Set click listener for button
mSpeakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mResultTextView.setText("Speak now...");
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
mSpeechRecognizer.setRecognitionListener(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() {
mResultTextView.setText("Processing...");
}
@Override
public void onError(int error) {
mResultTextView.setText("ERROR: " + error);
}
@Override
public void onResults(Bundle results) {
List<String> allWords = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d(TAG, "onResults:\t" + allWords.toString());
String result = allWords.get(0);
mResultTextView.setText("RESULT: '" + result + "'");
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
String locale = Locale.GERMANY.toString().replace("_", "-");
Log.d(TAG, "Locale:\t" + locale);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, locale);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, locale);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
mSpeechRecognizer.startListening(intent);
}
});
}
}
I've also tried it where I'm only doing this:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, locale);
mSpeechRecognizer.startListening(intent);
But the result is the same -- getting 'English' translation of the German utterance.
Any pointers?