Android ApiDemos示例解析(46):App->Voice Recognition
来源:互联网 发布:golang os.exit 编辑:程序博客网 时间:2024/06/07 22:25
这个例子需要Android系统中安装了支持RecognizerIntent.ACTION_RECOGNIZE_SPEECH的应用,比如Google的 Voice Search应用。
模拟器上缺省没有安装,可以参见如何在Android emulator上安装 APK 在模拟器上安装一个Voice Search。
本例VoiceRecognition首先通过PackageManager检测本机是否安装了支持RecognizerIntent.ACTION_RECOGNIZE_SPEECH,如果有,则Enable Speak按钮,否则显示“Recognizer not present”
// 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"); }
如果本机上安装了Google的Voice Search,点击“Speak!”则会启动语音输入对话框:
Speak按钮对应的代码如下:
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);
采用的是startActivityForResult这是因为我们需要从语音输入对话框获取用户输入。RecognizerIntent.EXTRA_PROMPT定义对应框提示。 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM 为语音输入类型,这里使用自由格式,另外一种为WEB_SEARCH主要用于Web搜索。
下面代码响应从语音输入对话框返回值:
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)); }
在列表中显示由语音识别返回的文字:
注:这个例子尽管在模拟器上可以安装Google Voice Search,但似乎Voice Search不能正确运行,本例最好使用手机来测试。
- Android ApiDemos示例解析(46):App->Voice Recognition
- Android ApiDemos示例解析(22):App->Dialog
- Android ApiDemos示例解析(23):App->Intents
- Android ApiDemos示例解析
- Android ApiDemos示例解析(3): App->Activity->Animation
- Android ApiDemos示例解析(4):App->Activity->Custom Dialog
- Android ApiDemos示例解析(5):App->Activity->Custom Title
- Android ApiDemos示例解析(6):App->Activity->Dialog
- Android ApiDemos示例解析(7):App->Activity->Forwarding
- Android ApiDemos示例解析(8):App->Activity->Hello world
- Android ApiDemos示例解析(9):App->Activity->Persistent State
- Android ApiDemos示例解析(10):App->Activity->QuickContactsDemo
- Android ApiDemos示例解析(11):App->Activity->Receive Result
- Android ApiDemos示例解析(12):App->Activity->Redirection
- Android ApiDemos示例解析(13):App->Activity->Reorder Activities
- Android ApiDemos示例解析(14):App->Activity->Save & Restore State
- Android ApiDemos示例解析(15):App->Activity->SetWallpaper
- Android ApiDemos示例解析(16):App->Activity->Translucent
- 《深度探索C++对象模型》读书笔记
- Android引路蜂地图开发示例:本地查询
- 北京圣思源视频下载
- [centos6.2] 文本安装后安装桌面环境
- getCanonicalName getSimpleName getName 区别与应用
- Android ApiDemos示例解析(46):App->Voice Recognition
- UART串口通信
- Eclipse快捷键
- 最近开发UITableView调用自定义的UITableViewCell的方式适用于低版本
- C++ - 汉诺塔
- ACM竞赛之新人向导 (鉴于这么多人问如何自学)
- 构造解数独
- UVa 10562 - Undraw the Trees (不限制儿子个数的树)
- 和菜鸟一起学证券投资之股市常见概念公式2