有許多教程可以在線向Android應用程序添加語音識別。它們往往令人困惑,編碼的出版商從來不會提出問題。我需要一個帶有完整編碼的簡單教程,以將語音識別添加到我的應用中。如何:將語音命令轉換爲Android應用程序
回答
如果你想添加語音識別到你的組的android應用程序,這是非常簡單的。
在整個教程中,您需要在粘貼代碼時添加導入。
- 創建一個XML文件,或使用一個現有的,並確保你添加一個按鈕和一個ListView。
- 您需要在java類中擴展活動並實現OnClickListener 您將看到一個錯誤,指出您有未實現的方法。將鼠標懸停在其上並添加未實現的方法。我們將在稍後添加。
接下來在你的java中設置按鈕和listview。
public ListView mList; public Button speakButton;
還補充:
public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
接下來,做出onCreate方法和設置按鈕和監聽。
speakButton = (Button) findViewById(R.id.btn_speak); speakButton.setOnClickListener(this);
也加入這個方法(我們將它設置旁邊)
voiceinputbuttons();
記住的setContentView爲你呈現的XML。
你的oncreate之外做一個新的方法,看起來像這樣。現在
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); }
接下來,從第2步你的onclick方法內從第6步
startVoiceRecognitionActivity();
接下來我們將不得不建立另一種方法添加的活性。複製並粘貼以下代碼。
@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隨時格式化代碼。
現在我們將在步驟8中設置我們的代碼使用的代碼。此代碼創建一個意圖,將用戶引導至新菜單。你將需要另外一個xml和java類。另外,請記住將活動添加到清單。
public void informationMenu() { startActivity(new Intent("android.intent.action.INFOSCREEN")); }
最後,您需要設置一些代碼,讓用戶知道麥克風是否可操作。將此代碼粘貼到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"
它看起來像聲音不工作它應該是「標準」
- 1. 如何使用python將語音命令程序製作爲android?
- 2. 如何創建一個Android應用程序將文本轉換爲語音?
- 3. Android音樂播放器應用程序的語音命令
- 4. 如何將AngularJS應用程序轉換爲android應用程序
- 5. 如何將j2me應用程序轉換爲android應用程序?
- 6. 用語音命令啓動android應用程序
- 7. 在語音命令上啓動應用程序android
- 8. Android語音命令
- 9. 如何將語音轉換爲文本?
- 10. Android應用程序的語音到文本轉換
- 11. 將Web應用程序轉換爲Android應用程序
- 12. 將GWT應用程序轉換爲Android應用程序。
- 13. 將Web應用程序轉換爲Android應用程序
- 14. 將Android應用程序轉換爲網絡應用程序
- 15. 將Android應用程序轉換爲Java桌面應用程序
- 16. 使用語音命令編程iphone應用程序
- 17. 如何將iPhone應用程序轉換爲iPad應用程序
- 18. 如何將ios應用程序轉換爲Xamarin.ios應用程序?
- 19. 文本到語音轉換爲web應用程序
- 20. 語音識別命令Android
- 21. 如何將Android應用程序的代碼轉換爲symbian
- 22. 如何將java swing應用程序轉換爲android
- 23. 如何將我的網站轉換爲Android應用程序
- 24. 如何將我的網站wordpress轉換爲android應用程序?
- 25. 將Unix命令轉換爲Windows命令
- 26. 如何將XML文件轉換爲Groovy語法從命令行
- 27. 使用語音命令在android中打開另一個應用程序?
- 28. Windows Phone啓動應用程序與語音命令在Cortana
- 29. 在語音命令(安卓)上啓動應用程序
- 30. 如何將命令提示符命令轉換爲.bat文件
你是真棒....好教程 – Pragnani 2013-01-21 05:09:36
@Sam這對互聯網連接很好。但是,有沒有像他沒有互聯網連接?我想要它的簡單命令,如打開消息框''打開瀏覽器''打開撥號鍵盤等。 – stack 2013-12-12 11:49:59
@stack使用Google Now集成? – milosmns 2014-08-31 21:16:37