java - 单击按钮时语音识别 Intent 未打开

标签 java android speech-recognition text-to-speech speech-to-text

我正在开发一个简单的助手应用程序,最初通过对其回复内容进行硬编码。它有一个按钮,单击后会显示语音识别器 Intent 。但现在单击按钮时不显示。我已在此处附加了我的代码,请帮助我找到导致错误的错误。

还请帮助我如何在不点击按钮的情况下调用语音识别器,只需说出“Ok google”中的一些指定单词即可。

MainActivity.java

package com.example.rv00485448.neha1;

import android.content.Intent;
import android.os.Build;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.Locale;

import static android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH;

public class MainActivity extends Activity{


    private TextToSpeech tts;
    private ArrayList<String> questions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

        findViewById(R.id.microphoneButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listen();
            }
        });
        loadQuestions();

        tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = tts.setLanguage(Locale.US);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "This Language is not supported");
                    }
                    speak("Hello");

                } else {
                    Log.e("TTS", "Initilization Failed!");
                }
            }
        });

    }

    private void loadQuestions(){
        questions = new ArrayList<>();
         questions.clear();
        questions.add("hi how are you");
        questions.add("I am good. how are you feeling today?");
        questions.add("Do you have vitals readings?");
        questions.add("you seem to have fever. Do you want me to book an appointment with doctor nandan ");
        questions.add("I have booked an appointment with doctor nandan at 5 PM");
        questions.add("Thank you too");
   }

    private void listen(){
        Intent i = new Intent(ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
     //   speak("I am listening to you");
 //        try {
//            startActivityForResult(i, 100);
 //        } catch (ActivityNotFoundException a) {
//            Toast.makeText(MainActivity.this, "Your device doesn't support Speech Recognition", Toast.LENGTH_SHORT).show();
//        }

}



    @Override
    public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    private void speak(String text){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);

        }else{
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
         super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> res = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String inSpeech = res.get(0);
                recognition(inSpeech);
            }
        }
    }

    private void recognition(String text){

        switch (text)
        {
            case "hello":
            {
                speak(questions.get(0));
                break;
            }

            case "fine how about you":
            {
                speak(questions.get(1));
                break;
            }

            case "feeling dizzy":
            {
                speak(questions.get(2));
                break;
             }

               case "yeah":
                 {
                    speak(questions.get(3));
                    break;
                 }

            case "yes":
            {
                speak(questions.get(4));
                break;
            }

            case "thank you":
            {
                speak(questions.get(5));
                 break;
            }

        }
    }
}

最佳答案

您没有触发该 Activity 。

在listen方法中使用startActivityForResult(i)。

关于java - 单击按钮时语音识别 Intent 未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42361226/

相关文章:

java - 在非常短的时间内在 Java 中搜索非常大的 ARPA 文件

android - 带标签的 DialogFragment 显示无法正常工作

java.lang.IllegalStateException - 为什么??? (安卓,列表适配器)

android - 尝试使用 SpeechRecognizer(Android 语音识别)但它没有启动?

java - 如何获取内部泛型类型使用的注释?

java - 当我的构造函数 Select(By) 未定义时,如何从 Selenium POM 中选择下拉菜单?

java - 如何创建 Spinner 以显示当前和接下来的 30 个日期

python-3.x - librosa.util.exceptions.ParameterError : Invalid shape for monophonic audio: ndim=2, shape=(172972, 2)

java - 获取两个音频输入流之间的差异 - java

java - 从 Java 读取 Windows ACL