智能助手

问题描述

我目前在Android Studio中设计了一个智能助手,并且正在我的App上运行,与用户的语音通信对于该App的生产至关重要,因此此功能的工作至关重要。

我想知道是否可以通过说出“ Alexa”(一个醒来的词)来打开语音助手,以了解我们如何与Alexa通信。

如下面的代码部分所示,我使用图像在单击时激活了智能助手-然后用户进行了相应的对话。如上所述,有没有一种方法可以使用户只需说点什么,而不用单击图像(imgspeech)。

感谢您的时间。谢谢。

这是我的.java文件

private TextToSpeech tts;
private ArrayList<String> questions;
private String name,surname,age,asName;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private static final String PREFS = "prefs";
private static final String NEW = "new";
private static final String NAME = "name";
private static final String AGE = "age";
private static final String AS_NAME = "as_name";
private static final String SURNAME = "surname";
private static final String COLOUR = "colour";
private ImageView imgbtnTorF,imgbtnQuizzes;
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getwindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_homescreen);
    imgbtnQuizzes = (ImageView) findViewById(R.id.imgBtnQuizzes);
    imgbtnTorF = (ImageView) findViewById(R.id.imgBtnTorF);
    preferences = getSharedPreferences(PREFS,0);
    editor = preferences.edit();

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

    tts = new TextToSpeech(this,new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                int result = tts.setLanguage(Locale.UK);
                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","Initialization Failed!");
            }
        }
    });
}

private void loadQuestions() {
    questions = new ArrayList<>();
    questions.clear();
    questions.add("Hello,what is your name?");
    questions.add("What is your surname?");
    questions.add("How old are you?");
    questions.add("Where do you live?");
    questions.add("What is your favourite colour?");
    questions.add("What is your favourite password you use?");
    questions.add("Remember if you are wondering what you can ask me,you can say the word @R_437_4045@ion and i will help you");
    questions.add("That's all I had,thank you ");
}

private void listen() {
    Intent i = new Intent(RecognizerIntent.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");

    try {
        startActivityForResult(i,100);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(homescreen.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,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) {
    Log.e("Speech","" + text);
    String[] speech = text.split(" ");
    if (text.contains("hello")) {
        speak(questions.get(0));
    }
    //
    if (text.contains("my name is")) {
        name = speech[speech.length - 1];
        Log.e("THIS","" + name);
        editor.putString(NAME,name).apply();
        speak(questions.get(1));
    }
    if (text.contains("my last name is")) {
        surname = speech[speech.length - 1];
        Log.e("THIS","" + surname);
        editor.putString(SURNAME,surname).apply();
        speak(questions.get(2));
    }
    //This must be the age
    if (text.contains("years") && text.contains("old")) {
        String age = speech[speech.length - 3];
        Log.e("THIS","" + age);
        editor.putString(AGE,age).apply();
        speak(questions.get(3));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)