智能助手

1sbrub3j  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(333)

我目前已经在android工作室设计了一个智能助手,它正在我的应用程序中工作,与用户的语音通信在这个应用程序的制作中是必不可少的,所以这个功能的工作是至关重要的。
我想知道是否有可能打开语音助手一样,我们可以与亚历克斯沟通说'亚历克斯'——一个唤醒词。
如下面的代码部分所示,我使用一个图像在单击时激活智能助手,然后用户相应地进行了对话。有没有一种方法可以像上面提到的那样,让用户可以简单地说点什么,而不是点击图片(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 information 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, 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) {
    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));

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题