Android语音识别方法
- android语音识别方法一:使用intent调用语音识别程序
1.
说明
以下例程功能为:在应用程序中使用intent来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。
2.
本例参考自android例程:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java
3.
可从此处下载可独立运行的代码:
4.
核心代码及说明
- packagecom.android.mystt1;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.content.pm.PackageManager;
- importandroid.content.pm.ResolveInfo;
- importandroid.os.Bundle;
- importandroid.speech.RecognizerIntent;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.ArrayAdapter;
- importandroid.widget.Button;
- importandroid.widget.ListView;
- importjava.util.ArrayList;
- importjava.util.List;
- publicclassMyStt1ActivityextendsActivityimplementsOnClickListener{
- privatestaticfinalintVOICE_RECOGNITION_REQUEST_CODE=1234;
- privateListViewmList;//显示识别后字串的list控件
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ButtonspeakButton=(Button)findViewById(R.id.btn_speak);//识别按钮
- mList=(ListView)findViewById(R.id.list);
- PackageManagerpm=getPackageManager();
- Listactivities=pm.queryIntentActivities(
- newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);//本地识别程序
- //newIntent(RecognizerIntent.ACTION_WEB_SEARCH),0);//网络识别程序
- if(activities.size()!=0){
- speakButton.setOnClickListener(this);
- }else{//若检测不到语音识别程序在本机安装,测将扭铵置灰
- speakButton.setEnabled(false);
- speakButton.setText("Recognizernotpresent");
- }
- }
- publicvoidonClick(Viewv){
- if(v.getId()==R.id.btn_speak){
- startMysttActivityActivity();
- }
- }
- privatevoidstartMysttActivityActivity(){//开始识别
- Intentintent=newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speechrecognitiondemo");
- startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);
- //调出识别界面
- }
- @Override
- protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
- if(requestCode==VOICE_RECOGNITION_REQUEST_CODE&&resultCode==RESULT_OK){
- //Fillthelistviewwiththestringstherecognizerthoughtitcouldhaveheard
- ArrayListmatches=data.getStringArrayListExtra(
- RecognizerIntent.EXTRA_RESULTS);
- mList.setAdapter(newArrayAdapter(this,android.R.layout.simple_list_item_1,
- matches));
- }
- //语音识别后的回调,将识别的字串在list中显示
- super.onActivityResult(requestCode,resultCode,data);
- }
- }
- android语音识别方法二:应用程序自己调用语音识别库
1.
说明
以下例程功能为:应用程序自身调用语言识别函数,程序以循环方式等待录音并识别后的字串。
2.
本例参考自android代码:
frameworks/base/core/java/android/speech/srec/Recognizer.java中注释部分
3.
可从此处下载可独立运行的代码:代码在一楼
4.
核心代码及说明
- packagecom.android.mystt2;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.widget.Button;
- importandroid.widget.TextView;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.speech.srec.Recognizer;
- importandroid.speech.srec.MicrophoneInputStream;
- importjava.io.InputStream;
- importjava.io.IOException;
- importandroid.util.Log;
- publicclassMyStt2ActivityextendsActivityimplementsOnClickListener{
- privateTextViewmText;
- privatestaticfinalStringTAG="MyStt3Activity";
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ButtonspeakButton=(Button)findViewById(R.id.btn_speak);//识别扭按
- mText=(TextView)findViewById(R.id.text);//显示识别后的字串
- speakButton.setOnClickListener(this);
- }
- publicvoidonClick(Viewv){
- if(v.getId()==R.id.btn_speak){
- test();
- }
- }
- voidtest(){
- try{
- InputStreamaudio=newMicrophoneInputStream(11025,11025*5);//设置输入参数
- Stringcdir=Recognizer.getConfigDir(null);//获取语音识别配置目录
- Recognizerrecognizer=newRecognizer(cdir+"/baseline11k.par");
- Recognizer.Grammargrammar=recognizer.newGrammar(cdir
- +"/grammars/VoiceDialer.g2g");
- grammar.setupRecognizer();
- grammar.resetAllSlots();
- grammar.compile();
- recognizer.start();//开始识别
- while(true){//循环等待识别结果
- switch(recognizer.advance()){
- caseRecognizer.EVENT_INCOMPLETE:
- caseRecognizer.EVENT_STARTED:
- caseRecognizer.EVENT_START_OF_VOICING:
- caseRecognizer.EVENT_END_OF_VOICING:
- continue;//未完成,继续等待识别结果
- caseRecognizer.EVENT_RECOGNITION_RESULT:
- for(inti=0;i<recognizer.getResultCount();i++){
- Stringresult=recognizer.getResult(i,
- Recognizer.KEY_LITERAL);
- Log.d(TAG,"result"+result);
- mText.setText(result);
- }//识别到字串,显示并退出循环
- break;
- caseRecognizer.EVENT_NEED_MORE_AUDIO:
- recognizer.putAudio(audio)//需要更多音频数据;
- continue;
- default:
- break;
- }
- break;
- }
- recognizer.stop();
- recognizer.destroy();
- audio.close();//回收资源
- }catch(IOExceptione){
- Log.d(TAG,"error",e);
- mText.setText("error"+e);
- }
- }
- }
- 语音识别方法三:使用Service调用语音识别程序
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.
核心代码及说明
- packagecom.android.mystt3;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.speech.RecognitionListener;
- importandroid.speech.RecognizerIntent;
- importandroid.speech.SpeechRecognizer;
- importandroid.widget.Button;
- importandroid.widget.TextView;
- importjava.util.ArrayList;
- importandroid.util.Log;
- publicclassMyStt3ActivityextendsActivityimplementsOnClickListener{
- privateTextViewmText;
- privateSpeechRecognizersr;
- privatestaticfinalStringTAG="MyStt3Activity";
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ButtonspeakButton=(Button)findViewById(R.id.btn_speak);//识别按钮
- mText=(TextView)findViewById(R.id.text);//显示识别字串
- speakButton.setOnClickListener(this);
- sr=SpeechRecognizer.createSpeechRecognizer(this);//初始化识别工具,得到句柄
- sr.setRecognitionListener(newlistener());//注册回调类及函数
- }
- classlistenerimplementsRecognitionListener//回调类的实现
- {
- publicvoidonReadyForSpeech(Bundleparams)
- {
- Log.d(TAG,"onReadyForSpeech");
- }
- publicvoidonBeginningOfSpeech()
- {
- Log.d(TAG,"onBeginningOfSpeech");
- }
- publicvoidonRmsChanged(floatrmsdB)
- {
- Log.d(TAG,"onRmsChanged");
- }
- publicvoidonBufferReceived(byte[]buffer)
- {
- Log.d(TAG,"onBufferReceived");
- }
- publicvoidonEndOfSpeech()
- {
- Log.d(TAG,"onEndofSpeech");
- }
- publicvoidonError(interror)
- {
- Log.d(TAG,"error"+error);
- mText.setText("error"+error);
- }
- publicvoidonResults(Bundleresults)//返回识别到的数据
- {
- Stringstr=newString();
- Log.d(TAG,"onResults"+results);
- ArrayListdata=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
- for(inti=0;i<data.size();i++)
- {
- Log.d(TAG,"result"+data.get(i));
- str+=data.get(i);
- }
- mText.setText(str);//显示被识别的数据
- }
- publicvoidonPartialResults(BundlepartialResults)
- {
- Log.d(TAG,"onPartialResults");
- }
- publicvoidonEvent(inteventType,Bundleparams)
- {
- Log.d(TAG,"onEvent"+eventType);
- }
- }
- publicvoidonClick(Viewv){
- if(v.getId()==R.id.btn_speak){
- sr.startListening(newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
- }
- }
- }
更多相关文章
- 【Android(安卓)开发】: Android(安卓)消息处理机制之三: Handle
- 通过android代码获取android系统的imei、手机型号、手机品牌、an
- Android应用程序启动过程源代码分析
- Android(安卓)学习笔记——利用JNI技术在Android中调用、调试C++
- 获取Android(安卓)SDK 源代码并在Eclipse中关联查看的方法--转
- Android学习之ListView使用基础
- android经典优化点
- Android获取所有安装APP信息的详细代码
- 判断客户端类型