2012-07-20 92 views
1

我目前正在開發一款應用,並且我需要能夠讓用戶在必要時用除了語音之外的其他任何東西來控制應用。我收到了一些顯然來自android的語音識別代碼,我試着在我的應用程序中使用它。代碼本身看起來相當簡單,但我需要幫助理解幾個部分並在應用程序上實現它。我已經在Android設備上運行了該應用程序,當我使用該演講時,我收到了一個我可能說過的短語列表。我希望能夠設置和活動,如果其中一個結果是「關閉應用程序」,例如它會關閉應用程序。我想我可以通過爲語音識別器的結果做if語句來設置它。如果結果==關閉應用程序,然後運行關閉的活動。我沒有任何設置,因爲我不確定如何完全編寫它,但我認爲你們中的一些人會有一些想法。理解和實現安卓應用上的日食識別語音識別

package com.example.com.proto1; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.os.Bundle; 
import android.speech.RecognizerIntent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

import java.util.ArrayList; 
import java.util.List; 


    /** 
    * Sample code that invokes the speech recognition intent API. 
    */ 
    public class VoiceRecognition extends Activity implements OnClickListener { 

     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 

     private ListView mList; 

     /** 
     * Called with the activity is first created. 
     */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Inflate our UI from its XML layout description. 
      setContentView(R.layout.voice_recognition); 

      // Get display items for later interaction 
      Button speakButton = (Button) findViewById(R.id.btn_speak); 

      mList = (ListView) findViewById(R.id.list); 

      // Check to see if a recognition activity is present 
      PackageManager pm = getPackageManager(); 
      List<ResolveInfo> activities = pm.queryIntentActivities(
        new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
      if (activities.size() != 0) { 
       speakButton.setOnClickListener(this); 
      } else { 
       speakButton.setEnabled(false); 
       speakButton.setText("Recognizer not present"); 
      } 
     } 

     /** 
     * Handle the click on the start recognition button. 
     */ 
     public void onClick(View v) { 
      if (v.getId() == R.id.btn_speak) { 
       startVoiceRecognitionActivity(); 
      } 
     } 

     /** 
     * Fire an intent to start the speech recognition activity. 
     */ 
     private 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); 
     } 

     /** 
     * Handle the results from the recognition activity. 
     */ 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent 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<String> matches = data.getStringArrayListExtra(
         RecognizerIntent.EXTRA_RESULTS); 
       mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
         matches)); 
      } 

      super.onActivityResult(requestCode, resultCode, data); 

     } 
    } 

回答

3

這是結果,你已經從語音識別活動所收到的:

ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

matches是你明顯可以文本列表。如果你想用的時候說了一些神奇的短語關閉您的應用程序,你需要的是這樣的:

if (matches.contains("close")) { 
    finish(); 
} 
+0

謝謝,它的工作很好 – 2012-07-20 14:38:10

3

如果你想超越的字符串比較,你可以嘗試制止或拼音匹配。

爲了讓您的識別更加準確或者如果您有一些難以識別的單詞,您需要這樣做。

查看代碼here,它實現了詞幹和拼音匹配。代碼的其他部分使用匹配進行語音識別。如果你想知道所有的細節,還有這個book

+0

感謝您的信息。我打算給你投票,因爲這很有幫助。但現在,我只需要非常簡單的命令,所以我不需要這個。 – 2012-07-24 13:00:33

+0

即使對於簡單的命令,您也需要使用鍋爐板代碼。 – gregm 2013-02-22 15:12:14