0

我正在为我的应用程序进行语音识别,我尝试了这段代码,但在我的 logcat 中出现错误,因为意图不能在 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()));
         }
    }

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

4

2 回答 2

1

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

android 中的语音识别功能是使用 RecognizerIntent 实现的。在 Intent 中使用Recognizer类来调用语音 API。在这里,我们检查设备中的识别器,如果可用的话,语音到文本会发生,否则我们会显示一个显示“未找到识别器”的 toast '

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);
}
}

查看演示

于 2013-10-14T10:13:13.710 回答
1

我不知道您在尝试Intent为您创建SpeechRecognizerwith时是否朝着正确的方向前进RecognizerIntent.getVoiceDetailsIntent(getApplicationContext()),因为根据文档使用此方法存在一些限制。这些限制可能导致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);

正如这个SO 线程所建议的那样。

于 2013-10-14T10:07:55.250 回答