我有一个 Vuzix m300(更新 1.2),我正在尝试让我的应用程序通过语音控制运行。我真的找不到任何特定于 m300 的代码示例(我认为是因为它是新的?)。内置的语音识别器工作正常。但是当我尝试通过 android.speech.SpeechRecognizer 使用它时,我发现识别不可用......
我已经尝试了一些我在 Internet 上找到的代码,尽管一些代码应该可以在 m100 上运行。没有什么对我有用。
这是我的代码:
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);
boolean b = SpeechRecognizer.isRecognitionAvailable(getApplicationContext());
final List<ResolveInfo> services = getApplicationContext().getPackageManager().queryIntentServices(
new Intent(RecognitionService.SERVICE_INTERFACE), 0);
b = isPackageInstalled(this.getApplicationContext(), "com.google.android.googlequicksearchbox");
}
public static boolean isPackageInstalled(@NonNull final Context ctx, @NonNull final String packageName) {
try {
ctx.getApplicationContext().getPackageManager().getApplicationInfo(packageName, 0);
return true;
} catch (final PackageManager.NameNotFoundException e) {
return false;
}
b 始终为 false 并且 List services 为空......因此我认为 Vuzix 上没有安装 SpeechRecongnizer,但是有(Vuzix 内置的?)。我愿意接受任何建议!
编辑:我已经安装了 Google Now App 和 Google App,现在我可以启动 SpeechRecognizer。但出于某种原因,该应用程序不会对我的声音做出反应。一段时间后,我得到一个 SpeechRecognizer ERROR_SPEECH_TIMEOUT。相同的应用程序在我的 Android 手机上运行良好,所以我认为它与 Vuzix M300 有什么关系?我在 onCreate 中的代码:
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
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");
speechRecognizer.setRecognitionListener(prepareRegnitionListener());
speechRecognizer.startListening(speechRecognizerIntent);
其余的:
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
}
@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
}
};
}
调用 onReadyforSpeech 方法,但之后没有任何反应,然后抛出错误。