我的 Android 应用程序有问题。我的设备是 Vuzix M300,我正在尝试让语音控制正常工作。我的问题是,当我初始化我的 SpeechRecognizer 并调用 startListining 方法时,一切都会运行,预计没有语音被识别。我已经在 Android 手机上尝试了相同的代码,它工作得很好。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//grant access to internet
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//set layout
setContentView(R.layout.activity_main);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "de-DE");
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
speechRecognizer.setRecognitionListener(prepareRegnitionListener());
speechRecognizer.startListening(speechRecognizerIntent);
//startListening(0);
}
private RecognitionListener prepareRegnitionListener() {
// TODO Auto-generated method stub
return new RecognitionListener() {
@Override
public void onRmsChanged(float rmsdB) {
//Didn´t use
}
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d(MainActivity,"Completed speech recognition: Result: " + matches);
String match = matches.get(0);
}
@Override
public void onReadyForSpeech(Bundle params) {
Log.d(MainActivity, "ReadyforSpeech");
}
@Override
public void onPartialResults(Bundle partialResults) {
// Nothing
ArrayList<String> matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Log.d(MainActivity,"Completed speech recognition: Result: " + matches);
}
@Override
public void onEvent(int eventType, Bundle params) {
// Nothing
}
@Override
public void onError(int error) {
switch (error){
case SpeechRecognizer.ERROR_AUDIO:
Log.e(MainActivity,"Failed to recognize speech: Audio recording error.");
startListening(1000);
break;
case SpeechRecognizer.ERROR_CLIENT:
Log.e(MainActivity,"Failed to recognize speech: Insufficient permissions.");
startListening(1000);
break;
case SpeechRecognizer.ERROR_NO_MATCH:
Log.d(MainActivity,"Failed to recognize speech: No recognition results matched. Retrying...");
startListening(1000);
break;
default:
Log.e(MainActivity,"Failed to recognize speech. Unknown error" + error);
startListening(1000);
}
}
@Override
public void onEndOfSpeech() {
Log.d(MainActivity, "EndofSpeech");
}
@Override
public void onBufferReceived(byte[] buffer) {
//Didn´t use
}
@Override
public void onBeginningOfSpeech() {
Log.d(MainActivity, "beginnofSpeech");//Do something when speaking starts
}
};
}
private void startListening(int delay){
if(delay > 0){
Timer t = new Timer();
t.schedule(new TimerTask(){
@Override
public void run() {
mainHandler.post(new Runnable() {
@Override
public void run() {
speechRecognizer.startListening(speechRecognizerIntent);
Log.d(MainActivity,"Start delayed listening to speech");
speechRecognizer.stopListening();
}
});
}}
, delay);
}else{
speechRecognizer.startListening(speechRecognizerIntent);
Log.d(MainActivity,"Start instant listening to speech.");
}
}
我正在使用 Android 6 和 Vuzix 固件 1.2(最新版本)运行 Vuzix。
如果没有给出语音输入(但我给出),我会得到错误代码 ERROR_SPEECH_TIMEOUT,就像我会得到的一样。
我已经尝试了几件事,比如使用 startActivityForResult 方法来运行语音控制(结果相同......)