在Android上启动语音识别时出现空例外

时间:2013-10-14 09:41:03

标签: android speech-recognition

我正在为我的应用程序进行语音识别,我尝试了这段代码 我在我的logcat中收到错误,因为 intent不能在android

中出现异常
    public class SpeechRecognizerActivity extends Activity {
        /** Called when the activity is first created. */
        String a;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           final TextView txt=(TextView)findViewById(R.id.textView1);
            class MyRecognitionListener implements RecognitionListener {

                            @Override
                            public void onBeginningOfSpeech() {
                                    Log.d("Speech", "onBeginningOfSpeech");
                            }

                            @Override
                            public void onBufferReceived(byte[] buffer) {
                                    Log.d("Speech", "onBufferReceived");
                            }

                            @Override
                            public void onEndOfSpeech() {
                                    Log.d("Speech", "onEndOfSpeech");
                            }

                            @Override
                            public void onError(int error) {
                                    Log.d("Speech", "onError");
                            }

                            @Override
                            public void onEvent(int eventType, Bundle params) {
                                    Log.d("Speech", "onEvent");
                            }

                            @Override
                            public void onPartialResults(Bundle partialResults) {
                                    Log.d("Speech", "onPartialResults");
                            }

                            @Override
                            public void onReadyForSpeech(Bundle params) {
                                    Log.d("Speech", "onReadyForSpeech");
                            }

                            @Override
                            public void onResults(Bundle results) {
                                    Log.d("Speech", "onResults");
                                    ArrayList<String> strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                                    for (int i = 0; i < strlist.size();i++ ) {
                                            Log.d("Speech", "result=" + strlist.get(i));
                                    }

                            }

                            @Override
                            public void onRmsChanged(float rmsdB) {
                                    Log.d("Speech", "onRmsChanged");
                            }

            }
            SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
            MyRecognitionListener listener = new MyRecognitionListener();
            sr.setRecognitionListener(listener);
   sr.startListening(RecognizerIntent.getVoiceDetailsIntent(getApplicationContext()));
         }
    }

请建议我,我犯了什么错误或者必须先设置一些设置 运行程序之前的模拟器。

2 个答案:

答案 0 :(得分:1)

在尝试使用IntentSpeechRecognizer创建RecognizerIntent.getVoiceDetailsIntent(getApplicationContext())时,我不知道您是否朝着正确的方向前进,因为根据此方法使用此方法有一些限制到documentation。这些限制可能导致函数返回null

请尝试以下方法:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); // Replace by your package.
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
sr.startListening(intent);

this SO线程建议。

答案 1 :(得分:1)

不知道您是否正确理解了VoiceRecognization。但谷歌提供了使用语音命令搜索网络的选项。

Android中的语音识别是使用 RecognizerIntent 实现的。使用识别器类来调用语音API。我们检查设备中的识别器是否可用语音文本发生否则我们会显示一个显示“未找到识别器”的吐司

private void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "AndroidBite Voice Recognition...");
startActivityForResult(intent, REQUEST_CODE);
}
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
     if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
     {
         ArrayList<String> matches = data.getStringArrayListExtra(
          RecognizerIntent.EXTRA_RESULTS);
           resultList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
      }
   super.onActivityResult(requestCode, resultCode, data);
}
}

查看demo