http://www.apkbus.com/forum.php?mod=viewthread&tid=3473
1.
说明
以下例程功能为:在应用程序中使用intent来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。
2.
本例参考自android例程:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java
3.
可从此处下载可独立运行的代码:
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); } }
1.
说明
以下例程功能为:应用程序自身调用语言识别函数,程序以循环方式等待录音并识别后的字串。
2.
本例参考自android代码:
frameworks/base/core/java/android/speech/srec/Recognizer.java中注释部分
3.
可从此处下载可独立运行的代码:代码在一楼
4.
核心代码及说明
package com.android.mystt2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; import android.view.View; import android.view.View.OnClickListener; import android.speech.srec.Recognizer; import android.speech.srec.MicrophoneInputStream; import java.io.InputStream; import java.io.IOException; import android.util.Log; public class MyStt2Activity extends Activity implements OnClickListener { private TextView mText; private static final String TAG = "MyStt3Activity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button speakButton = (Button) findViewById(R.id.btn_speak); // 识别扭按 mText = (TextView) findViewById(R.id.text); // 显示识别后的字串 speakButton.setOnClickListener(this); } public void onClick(View v) { if (v.getId() == R.id.btn_speak) { test(); } } void test() { try { InputStream audio = new MicrophoneInputStream(11025, 11025 * 5); //设置输入参数 String cdir = Recognizer.getConfigDir(null); // 获取语音识别配置目录 Recognizer recognizer = new Recognizer(cdir + "/baseline11k.par"); Recognizer.Grammar grammar = recognizer.new Grammar(cdir + "/grammars/VoiceDialer.g2g"); grammar.setupRecognizer(); grammar.resetAllSlots(); grammar.compile(); recognizer.start(); // 开始识别 while (true) { // 循环等待识别结果 switch (recognizer.advance()) { case Recognizer.EVENT_INCOMPLETE: case Recognizer.EVENT_STARTED: case Recognizer.EVENT_START_OF_VOICING: case Recognizer.EVENT_END_OF_VOICING: continue; // 未完成,继续等待识别结果 case Recognizer.EVENT_RECOGNITION_RESULT: for (int i = 0; i < recognizer.getResultCount(); i++) { String result = recognizer.getResult(i, Recognizer.KEY_LITERAL); Log.d(TAG, "result " + result); mText.setText(result); } // 识别到字串,显示并退出循环 break; case Recognizer.EVENT_NEED_MORE_AUDIO: recognizer.putAudio(audio) // 需要更多音频数据; continue; default: break; } break; } recognizer.stop(); recognizer.destroy(); audio.close(); // 回收资源 } catch (IOException e) { Log.d(TAG, "error", e); mText.setText("error " + e); } } }
1.
说明
以下例程功能为:在应用程序中使用通于访问service调用语言识别功能,录音并识别后将识别的字串通过Listener返回给应用程序。注意:使用前需要安装语音识别服务,如编译安装源码中的development/samples/VoiceRecogitionService。
2.
本例参考自android源码
a)
后台服务
参见development/samples/VoiceRecognitionService/*
此处实现了一个模拟的后台服务,它并未实现真的语音识别,而只是一个框架以示例,编译并安装它,即可在设置的语音输入与输出中看到它,它包含了一个设置界面,当连接这个Service时,如果设置了Letters,则直接返回abc,如果设置了Numbers,则直接返回123
你可以自己实现,用于连接android源码自带的识别引擎srec.
b)
前台程序
参见frameworks/base/core/java/android/speech/Recognition*
它与后台Service交互,此段代码实现在应用程序界面中
3.
可从此处下载可独立运行的代码(前台程序):源代码在一楼
4.
核心代码及说明
package com.android.mystt3; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; import android.util.Log; public class MyStt3Activity extends Activity implements OnClickListener { private TextView mText; private SpeechRecognizer sr; private static final String TAG = "MyStt3Activity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button speakButton = (Button) findViewById(R.id.btn_speak); // 识别按钮 mText = (TextView) findViewById(R.id.text); // 显示识别字串 speakButton.setOnClickListener(this); sr = SpeechRecognizer.createSpeechRecognizer(this); // 初始化识别工具,得到句柄 sr.setRecognitionListener(new listener()); // 注册回调类及函数 } class listener implements RecognitionListener // 回调类的实现 { public void onReadyForSpeech(Bundle params) { Log.d(TAG, "onReadyForSpeech"); } public void onBeginningOfSpeech() { Log.d(TAG, "onBeginningOfSpeech"); } public void onRmsChanged(float rmsdB) { Log.d(TAG, "onRmsChanged"); } public void onBufferReceived(byte[] buffer) { Log.d(TAG, "onBufferReceived"); } public void onEndOfSpeech() { Log.d(TAG, "onEndofSpeech"); } public void onError(int error) { Log.d(TAG, "error " + error); mText.setText("error " + error); } public void onResults(Bundle results) // 返回识别到的数据 { String str = new String(); Log.d(TAG, "onResults " + results); ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); for (int i = 0; i < data.size(); i++) { Log.d(TAG, "result " + data.get(i)); str += data.get(i); } mText.setText(str); // 显示被识别的数据 } public void onPartialResults(Bundle partialResults) { Log.d(TAG, "onPartialResults"); } public void onEvent(int eventType, Bundle params) { Log.d(TAG, "onEvent " + eventType); } } public void onClick(View v) { if (v.getId() == R.id.btn_speak) { sr.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)); } } }