Google的語音識別是有目共睹的,所以Android上面也是沾了大光了,用起來簡單至極。
過程如下:
1、啟動語音識別Activity
2、這里處理語音(傳到google服務器處理)
3、結果以Acitivity的結果返回(onActivityResult)
主要用到的類為[android.speech.RecognizerIntent](http://developer.android.com/reference/android/speech/RecognizerIntent.html)
下面的例子參考了API Demo。
~~~
package?com.linc;??
??
import?java.util.ArrayList;??
import?java.util.List;??
??
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.Button;??
import?android.widget.TextView;??
??
public?class?VoiceRecognitionDemoActivity?extends?Activity?{??
????private?static?final?String?TAG?=?"VoiceRecognition";??
????private?static?final?int?VOICE_RECOGNITION_REQUEST_CODE?=?1234;??
??????
????private?TextView?textView;??
????private?Button?button;??
????@Override??
????public?void?onCreate(Bundle?savedInstanceState)?{??
????????super.onCreate(savedInstanceState);??
????????setContentView(R.layout.main);??
??????????
????????initWidget();??
??????????
????????//?Check?to?see?if?a?recognition?activity?is?present??
????????PackageManager?pm?=?getPackageManager();??
????????List?activities?=?pm.queryIntentActivities(??
????????????????new?Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),?0);??
????????if?(activities.size()?!=?0)?{??
????????????button.setOnClickListener(new?OnClickListener()?{??
????????????????@Override??
????????????????public?void?onClick(View?v)?{??
????????????????????startVoiceRecognitionActivity();??
????????????????}??
????????????});??
????????}?else?{??
????????????button.setEnabled(false);??
????????????button.setText("Recognizer?not?present");??
????????}??
????}??
??????
????private?void?initWidget()??
????{??
????????textView?=?(TextView)findViewById(R.id.tv);??
????????button?=?(Button)findViewById(R.id.btn);??
????}??
??????
????/**?
?????*?Fire?an?intent?to?start?the?speech?recognition?activity.?
?????*/??
????private?void?startVoiceRecognitionActivity()?{??
????????Intent?intent?=?new?Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);??
??
????????//?Display?an?hint?to?the?user?about?what?he?should?say.??
????????intent.putExtra(RecognizerIntent.EXTRA_PROMPT,?"請說標準普通話");//注意不要硬編碼??
??
????????//?Given?an?hint?to?the?recognizer?about?what?the?user?is?going?to?say??
????????intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,??
????????????????RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);??
??
????????//?Specify?how?many?results?you?want?to?receive.?The?results?will?be?sorted??
????????//?where?the?first?result?is?the?one?with?higher?confidence.??
????????intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,?5);//通常情況下,第一個結果是最準確的。??
??
????????startActivityForResult(intent,?VOICE_RECOGNITION_REQUEST_CODE);??
????}??
??????
????@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?matches?=?data.getStringArrayListExtra(??
????????????????????RecognizerIntent.EXTRA_RESULTS);??
????????????StringBuilder?stringBuilder?=?new?StringBuilder();??
????????????int?Size?=?matches.size();???
????????????for(int?i=0;i
????????????{??
????????????????stringBuilder.append(matches.get(i));??
????????????????stringBuilder.append("\n");??
????????????}??
????????????textView.setText(stringBuilder);??
????????}??
??
????????super.onActivityResult(requestCode,?resultCode,?data);??
????}??
}??
~~~
結論:
在wifi(家里的,1兆帶寬)狀態下,識別速度飛快。
語音包是買手機時預裝好的。
識別率很高。中文、英文、數字都能很好的識別。
用手機的gprs,效果不盡如人意。幾次識別都用了30秒左右的時間,體驗很糟。
- 前言
- 一:文本與布局
- 二:組合控件
- 三:性能測試類
- 四:語音識別
- 五:讀取Excel
- 六:PreferenceActivity使用詳解
- 七:按鈕控制ViewPager的左右翻頁
- 八:Ubuntu下切換JDK版本
- 九:最新Android開發環境(Eclipse+ADT+Android 5.0)
- 十:獲得屏幕物理尺寸、密度及分辨率
- 十一:Android Studio和Gradle
- 十二:Android Studio導入第三方類庫、jar包和so庫
- 十三:APK簽名
- 十四:混淆與反編譯
- 十五:多分辨率適配常用目錄
- 十六:getprop與dumpsys命令
- 十七:Linux下的模擬器硬件加速
- 十八:adb取出安裝在手機中的apk
- 十九:android studio導出jar包(Module)并獲得手機信息
- 二十:兩個開源的圖表/報表控件
- 二十一:Android原型設計工具探索
- 二十二:Android 5.1 SDK下載與配置
- 二十三:Android Studio的NDK開發
- 二十四:橫豎屏切換
- 二十五:模擬器如何重啟?試試Genymotion!
- 二十六:persistableMode與Activity的持久化
- 二十七:Maven編譯開源二維碼掃描項目zxing