2012-08-03 87 views

回答

74

如果你想添加語音識別到你的組的android應用程序,這是非常簡單的。

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

  1. 創建一個XML文件,或使用一個現有的,並確保你添加一個按鈕和一個ListView。
  2. 您需要在java類中擴展活動並實現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(); 
    

    記住的setContentView爲你呈現的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)); 
    

    匹配是語音輸入的結果。這是用戶可能說的內容的列表。對你想使用的關鍵字使用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(); 
     } 
    } 
} 
+1

你是真棒....好教程 – Pragnani 2013-01-21 05:09:36

+5

@Sam這對互聯網連接很好。但是,有沒有像他沒有互聯網連接?我想要它的簡單命令,如打開消息框''打開瀏覽器''打開撥號鍵盤等。 – stack 2013-12-12 11:49:59

+0

@stack使用Google Now集成? – milosmns 2014-08-31 21:16:37

5

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

要完成多一點點:

您需要的權限添加到您的清單如下

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

此外,如果有

launchMode="singleInstance"launchMode="singleTask"它看起來像聲音不工作它應該是「標準」

相關問題