android语音识别方法一:使用intent调用语音识别程序
android 语音识别方法一:使用 intent 调用语音识别程序
#移动开发 #Android
1. 说明
以下例程功能为:在应用程序中使用 intent
来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。
2. 本例参考自 android 例程: development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java
3. 可从此处下载可独立运行的代码: [http://download.csdn.net/source/2591401
](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/
) _