Android RecognizerIntent语音识别

use*_*988 1 android speech-recognition recognizer-intent

如果由于用户不说话而在RecognizerIntent完成的情况下如何处理图像(ImageView)的可见性

if (RecognizerIntent.EXTRA_RESULTS == null){
image1.setVisibility(View.VISIBLE);///microphone icon
}
Run Code Online (Sandbox Code Playgroud)

要么

if (RecognizerIntent.ACTION_RECOGNIZE_SPEECH == null){
image1.setVisibility(View.INVISIBLE);///microphone
}
Run Code Online (Sandbox Code Playgroud)

日Thnx

com*_*oro 12

在上面的代码中,您只是测试常量是否为空,而不是.你必须在代码中的某处开始识别

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //... put other settings in the Intent 
    startActivityForResult(intent, REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

并收到结果

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data)
     {
       if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
         ArrayList<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
          //... do your stuf with results
         }
     super.onActivityResult(requestCode, resultCode, data);
     }
Run Code Online (Sandbox Code Playgroud)

更可定制的方法是直接使用SpeechRecognizer.例如,请参阅此问题.