语音指令到Android应用程序

语音指令到Android应用程序

本文介绍了如何:语音指令到Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多教程在线添加语音识别Android应用程序。他们往往混淆和编码的出版商从未提供的问题。我需要一个简单的教程,完整的编码添加语音识别我的应用程序。

There are many tutorials online for adding voice recognition to an android app. They are often confusing and the publishers of the coding are never available for questions. I need a simple tutorial with complete coding for adding voice recognition to my app.

推荐答案

如果你想语音识别添加到组的Andr​​oid应用程序是非常简单的。

If you want to add voice recognition to your group's android app it is very simple.

在本教程中,你需要为你在code粘贴增加进口。

Throughout this tutorial you will need to add imports as you paste in the code.

  1. 创建一个XML文件,或使用现有的,并确保你添加一个按钮和一个列表视图。
  2. 在您需要扩展活动并实现一个Java类OnClickListener你会得到一个错误,说你有没有实现的方法。悬停在它上面,并添加未实现的方法。我们将添加到这一点。
  3. 下一步设置按钮和ListView在Java。

  1. create an xml file or use an existing one and make sure that you add a button and a listview.
  2. in a java class you need to extend activity and implement OnClickListenerYou will get an error that says you have unimplemented methods. Hover over it and add the unimplemented methods. We will add to this later.
  3. Next set up the button and listview in your java.

public ListView mList;
public Button speakButton;

还补充:

public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

  • 接下来,一个onCreate方法和设置按钮和监听。

  • Next, make an OnCreate Method and set up the button and listener.

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

    也加入这个方法(我们将它设置旁边)

    also add this method(we will set it up next)

    voiceinputbuttons();
    

    记住的setContentView为您所呈现的XML。

    Remember to setContentView for the xml you are showing.

    您之外的OnCreate做出了新的方法,看起来是这样的。

    Outside of your oncreate make a new method that looks like this.

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

  • 现在你必须设置语音识别活动通过以下code。

  • Now you will have to set up your voice recognition activity by using the following code.

    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);
    }
    

  • 其次,从第2步你的onclick方法内部步骤6中添加活性。

  • Next, inside your onclick method from step 2 add the activity from step 6.

    startVoiceRecognitionActivity();
    

  • 接下来我们将不得不建立另一种方法。复制并粘贴以下code。

  • Next we will have to set up another method. Copy and paste the following code.

    @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));
    

    匹配的语音输入的结果。这是一个什么样的用户可能说的列表。使用if语句为您要使用的关键字允许使用的任何活动,如果关键字匹配,可以设置多个关键字使用相同的活动,以便多个单词将允许用户使用活动(使得它如此用户不必记住从列表话)要使用的活性从语音输入信息简单地使用以下格式;

    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("information")) {
        informationmenu();
    }
    

    请注意:您可以格式化code任何时候通过pressing Ctrl + Shift + F在eclipse

    NOTE: you can format the code any time by pressing ctrl+shift+F in eclipse.

    现在我们要建立我们所用的code方法步骤8.本code创建一个旨在将用户定向到一个新的菜单。您将需要另一个XML和Java类此。此外,请记住到活动添加到您的清单。

    Now we are going to set up our method used by the code in step 8. This code creates an intent to direct the user to a new menu. You will need another xml and java class for this. Also, remember to add an activity to your manifest.

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

  • 最后,您需要设置一些code,这将让用户知道如果麦克是可操作的。在最后的onCreate方法里面粘贴此code。

  • Finally you need to set up some code that will let the user know if the mic is operational. Paste this code inside of the OnCreate method at the end.

    // 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");
    }
    

  • 最后要注意的:语音识别将不能在虚拟仿真工作,因为他们无法访问麦克风您的计算机上。语音识别将只与互联网连接工作。

    FINAL NOTE: Voice Recognition will not work on a virtual emulator because they can't access the mic on your computer. The voice recognition will only work with an internet connection.

    这是约。最终code应该是什么样的你的java。

    This is approx. what your final code should look like in your 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();
            }
        }
    }
    

    这篇关于如何:语音指令到Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-03 11:40