我正在使用SpeechRecognizer
尝试获取一些德语字符串。我正在通过Google Translate德语录音(例如,“你好吗?” - >“Wie geht es Ihnen?”)。
我看过几个关于如何改变语言以接受除英语之外的其他语言的问题/答案,但似乎都没有。
在将设备的语言设置保持为英语(en-US)的同时,是否有人知道如何执行此操作?
只有当我将设备的语言设置为“de-DE”时,我才能成功获取德语字符串。
这是我的示例代码:
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);
}
});
}
}
我也尝试过,我只会这样做:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, locale);
mSpeechRecognizer.startListening(intent);
但结果是一样的 - 获得德语言的'英语'翻译。
任何指针?