首页 文章

Cordova语音识别没有标准的谷歌弹出界面

提问于
浏览
-1

我正在尝试开发具有语音识别功能的cordova应用程序,但我不想使用标准的Google弹出界面 . 我基本上使用语音识别插件:http://plugins.cordova.io/#/package/com.manueldeveloper.speech-recognizer,但我想设置一个RecognitionListener并将其用于识别回调 . 这在使用本教程中的代码开发的本机应用程序中完美运行:http://www.truiton.com/2014/06/android-speech-recognition-without-dialog-custom-activity/ . 所以我尝试在插件中开发一个类似的解决方案:

Activity a = cordova.getActivity();

SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(a);
sr.setRecognitionListener(new listener());

// Create the intent and set parameters


Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);

if (maxMatches > 0)
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxMatches);
if (!prompt.equals(""))
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);

sr.startListening(intent);

cordova.startActivityForResult(this, intent, REQUEST_CODE);

当我尝试启动语音识别时,我收到一个错误:“SpeechRecognizer应该仅从应用程序的主线程中使用” . 这对于开发本机android代码的人来说应该不是一个大问题,但我实际上是使用phonegap来避免原生的android代码 .

2 回答

相关问题