android语音识别方法一:使用intent调用语音识别程序
2010-08-03 13:26
716 查看
1. 说明
以下例程功能为:在应用程序中使用intent来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。
2. 本例参考自android例程:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java
3. 可从此处下载可独立运行的代码:
http://download.csdn.net/source/2591401
4. 核心代码及说明
package com.android.mystt1;
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;
public class MyStt1Activity extends Activity implements OnClickListener {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private ListView mList; // 显示识别后字串的list控件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button speakButton = (Button) findViewById(R.id.btn_speak); // 识别按钮
mList = (ListView) findViewById(R.id.list);
PackageManager pm = getPackageManager();
List activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); //本地识别程序
// new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0); // 网络识别程序
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else { // 若检测不到语音识别程序在本机安装,测将扭铵置灰
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
}
public void onClick(View v) {
if (v.getId() == R.id.btn_speak) {
startMysttActivityActivity();
}
}
private void startMysttActivityActivity() { // 开始识别
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);
// 调出识别界面
}
@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);
mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
matches));
}
// 语音识别后的回调,将识别的字串在list中显示
super.onActivityResult(requestCode, resultCode, data);
}
}
(转载请注明出处: http://xy0811.spaces.live.com/)
以下例程功能为:在应用程序中使用intent来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。
2. 本例参考自android例程:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java
3. 可从此处下载可独立运行的代码:
http://download.csdn.net/source/2591401
4. 核心代码及说明
package com.android.mystt1;
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;
public class MyStt1Activity extends Activity implements OnClickListener {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private ListView mList; // 显示识别后字串的list控件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button speakButton = (Button) findViewById(R.id.btn_speak); // 识别按钮
mList = (ListView) findViewById(R.id.list);
PackageManager pm = getPackageManager();
List activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); //本地识别程序
// new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0); // 网络识别程序
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else { // 若检测不到语音识别程序在本机安装,测将扭铵置灰
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
}
public void onClick(View v) {
if (v.getId() == R.id.btn_speak) {
startMysttActivityActivity();
}
}
private void startMysttActivityActivity() { // 开始识别
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);
// 调出识别界面
}
@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);
mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
matches));
}
// 语音识别后的回调,将识别的字串在list中显示
super.onActivityResult(requestCode, resultCode, data);
}
}
(转载请注明出处: http://xy0811.spaces.live.com/)
相关文章推荐
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法一:使用intent调用语音识别程序
- 语音识别方法三:使用Service调用语音识别程序
- 语音识别方法三:使用Service调用语音识别程序
- 语音识别方法三:使用Service调用语音识别程序
- Unity-Android通信:AndroidJava 使用Unity c#编写Android程序调用任何方法
- Android 通过调用谷歌语音系统接口弹出语音识别对话框的方法 .
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- 关于在android程序执行过程中使用Intent启动另一个活动后,同个方法未执行的代码是否会继续执行。
- Android 通过调用谷歌语音系统接口弹出语音识别对话框的方法 .
- 【Android】使用Intent调用系统其它程序,使用onKeyDown对音量键的监听,长按事件
- Android 通过调用系统接口使用如 谷歌语音、百度语音、讯飞语音等语音识别对话框的方法
- Android 通过调用系统,如接口 谷歌语音、百度语音、科大讯飞语音等语音识别方法对话框
- Android 系统应用调用,intent 的使用方法总结
- 安卓调用系统语音识别功能全解(谷歌语音服务):获取识别结果,使用语音识别进行搜索。
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android学习笔记---53_采用网页设计软件界面,以及使用android系统内置的浏览器,利用js调用java方法