簡體   English   中英

如何:將語音命令輸入到 Android 應用程序中

[英]How To: Voice Commands into an android application

網上有很多教程可以將語音識別添加到 Android 應用程序。 它們常常令人困惑,並且編碼的發布者永遠無法回答問題。 我需要一個關於如何將語音識別添加到我的應用程序的基本概述(作為答案,而不是鏈接)。

如果您想將語音識別添加到您小組的 Android 應用程序中,這非常簡單。

在本教程中,您需要在粘貼代碼時添加導入。

  1. 創建一個 xml 文件或使用現有文件,並確保添加一個按鈕和一個列表視圖。
  2. 在 java 類中,您需要擴展活動並實現OnClickListener您將收到一條錯誤消息,提示您有未實現的方法。 將鼠標懸停在它上面並添加未實現的方法。 我們稍后會對此進行補充。
  3. 接下來在您的 java 中設置按鈕和列表視圖。

     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 設置內容視圖。

  5. 在你的 oncreate 之外創建一個看起來像這樣的新方法。

     public void voiceinputbuttons() { speakButton = (Button) findViewById(R.id.btn_speak); mList = (ListView) findViewById(R.id.list); }
  6. 現在您必須使用以下代碼設置您的語音識別活動。

     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 步的活動。

     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 語句允許使用任何活動,如果關鍵字匹配,可以設置多個關鍵字以使用相同的活動,這樣多個詞將允許用戶使用該活動(使其如此用戶不必記住列表中的單詞)要使用語音輸入信息中的活動,只需使用以下格式;

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

    注意:您可以隨時通過在 Eclipse 中按 ctrl+shift+F 來格式化代碼。

  9. 現在我們將設置第 8 步中的代碼使用的方法。此代碼創建一個意圖將用戶定向到新菜單。 為此,您將需要另一個 xml 和 java 類。 另外,請記住向您的清單添加一個活動。

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

真的很好的教程。 做得好。

要完成更多一點:

您需要向您的清單添加權限,如下所示

<uses-permission android:name="android.permission.RECORD_AUDIO" />

如果你有,語音也不起作用

launchMode="singleInstance"launchMode="singleTask"看起來應該是“標准”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM