gpt4 book ai didi

java - 如何: Voice Commands into an android application

转载 作者:IT老高 更新时间:2023-10-28 20:31:07 27 4
gpt4 key购买 nike

网上有很多教程可以将语音识别添加到安卓应用中。它们经常令人困惑,并且编码的发布者永远无法回答问题。我需要一个关于如何向我的应用添加语音识别的基本概述(作为答案,而不是链接)。

最佳答案

如果您想将语音识别添加到您小组的 Android 应用程序,这非常简单。

在本教程中,您需要在粘贴代码时添加导入。

  1. 创建一个 xml 文件或使用现有的文件,并确保添加一个按钮和一个 ListView 。
  2. 在一个java类中你需要扩展activity并实现OnClickListener你会得到一个错误,说你有未实现的方法。将鼠标悬停在它上面并添加未实现的方法。我们稍后会添加。
  3. 接下来在你的java中设置按钮和 ListView 。

    public ListView mList;
    public Button speakButton;

    还要补充:

    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
  4. 接下来,创建一个 OnCreate 方法并设置按钮和监听器。

    speakButton = (Button) findViewById(R.id.btn_speak);
    speakButton.setOnClickListener(this);

    也添加这个方法(我们接下来会设置它)

    voiceinputbuttons();

    记得为你显示的 xml 设置内容 View 。

  5. 在你的 oncreate 之外创建一个看起来像这样的新方法。

    public void voiceinputbuttons() {
    speakButton = (Button) findViewById(R.id.btn_speak);
    mList = (ListView) findViewById(R.id.list);
    }
  6. 现在您必须使用以下代码设置语音识别 Activity 。

    public 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,
    "Speech recognition demo");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
  7. 接下来,在第 2 步中的 onclick 方法中添加第 6 步中的 Activity 。

    startVoiceRecognitionActivity();
  8. 接下来我们将不得不设置另一个方法。复制并粘贴以下代码。

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
    ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));

    matches 是语音输入的结果。它是用户可能说的话的列表。对要使用的关键字使用 if 语句允许使用任何 Activity ,如果关键字匹配,可以设置多个关键字以使用相同的 Activity ,因此多个单词将允许用户使用 Activity (使其如此用户不必记住列表中的单词)要使用语音输入信息中的 Activity ,只需使用以下格式;

    if (matches.contains("information")) {
    informationMenu();
    }

    注意:你可以在eclipse中按ctrl+shift+F随时格式化代码。

  9. 现在我们将设置第 8 步中的代码使用的方法。此代码创建了一个将用户引导至新菜单的 Intent 。为此,您将需要另一个 xml 和 java 类。另外,请记住在 list 中添加一个 Activity 。

    public void informationMenu() {
    startActivity(new Intent("android.intent.action.INFOSCREEN"));
    }
  10. 最后,您需要设置一些代码,让用户知道麦克风是否正常工作。将此代码粘贴到最后的 OnCreate 方法中。

    // Check to see if a recognition activity is present
    // if running on AVD virtual device it will give this message. The mic
    // required only works on an actual android device
    PackageManager pm = getPackageManager();
    List activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() != 0) {
    voiceButton.setOnClickListener(this);
    } else {
    voiceButton.setEnabled(false);
    voiceButton.setText("Recognizer not present");
    }

最后说明:语音识别无法在虚拟模拟器上运行,因为它们无法访问您计算机上的麦克风。语音识别仅适用于互联网连接。

这是大约。你的最终代码在你的 java 中应该是什么样子。

package com.example.com.tutorialthread;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.speech.RecognizerIntent;
import android.support.v4.app.NavUtils;

public class main extends Activity implements OnClickListener {

public ListView mList;
public Button speakButton;

public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

speakButton = (Button) findViewById(R.id.btn_speak);
speakButton.setOnClickListener(this);

voiceinputbuttons();
}

public void informationMenu() {
startActivity(new Intent("android.intent.action.INFOSCREEN"));
}

public void voiceinputbuttons() {
speakButton = (Button) findViewById(R.id.btn_speak);
mList = (ListView) findViewById(R.id.list);
}

public 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,
"Speech recognition demo");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

public void onClick(View v) {
// TODO Auto-generated method stub
startVoiceRecognitionActivity();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it
// could have heard
ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, matches));
// matches is the result of voice input. It is a list of what the
// user possibly said.
// Using an if statement for the keyword you want to use allows the
// use of any activity if keywords match
// it is possible to set up multiple keywords to use the same
// activity so more than one word will allow the user
// to use the activity (makes it so the user doesn't have to
// memorize words from a list)
// to use an activity from the voice input information simply use
// the following format;
// if (matches.contains("keyword here") { startActivity(new
// Intent("name.of.manifest.ACTIVITY")

if (matches.contains("information")) {
informationMenu();
}
}
}

关于java - 如何: Voice Commands into an android application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11798337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com