Android语音识别方法

  • android语音识别方法一:使用intent调用语音识别程序

1.

说明
以下例程功能为:在应用程序中使用intent来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。

2.
本例参考自android例程:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java

3.
可从此处下载可独立运行的代码:
4.
核心代码及说明

Java代码   收藏代码
  1. package com.android.mystt1;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.pm.PackageManager;  
  6. import android.content.pm.ResolveInfo;  
  7. import android.os.Bundle;  
  8. import android.speech.RecognizerIntent;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.Button;  
  13. import android.widget.ListView;  
  14.    
  15. import java.util.ArrayList;  
  16. import java.util.List;  
  17.    
  18. public class MyStt1Activity extends Activity implements OnClickListener {  
  19.        private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  20.        private ListView mList;          // 显示识别后字串的list控件  
  21.    
  22.        @Override  
  23.        public void onCreate(Bundle savedInstanceState) {  
  24.                 super.onCreate(savedInstanceState);  
  25.                 setContentView(R.layout.main);  
  26.                 Button speakButton = (Button) findViewById(R.id.btn_speak); // 识别按钮  
  27.                  mList = (ListView) findViewById(R.id.list);  
  28.                 PackageManager pm = getPackageManager();  
  29.                 List activities = pm.queryIntentActivities(  
  30.                           new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); //本地识别程序  
  31. //                       new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0); // 网络识别程序  
  32.                 if (activities.size() != 0) {  
  33.                          speakButton.setOnClickListener(this);  
  34.                 } else {                 // 若检测不到语音识别程序在本机安装,测将扭铵置灰  
  35.                          speakButton.setEnabled(false);  
  36.                          speakButton.setText("Recognizer not present");  
  37.                 }  
  38.        }  
  39.    
  40.        public void onClick(View v) {  
  41.                 if (v.getId() == R.id.btn_speak) {  
  42.                          startMysttActivityActivity();  
  43.                 }  
  44.        }  
  45.    
  46.        private void startMysttActivityActivity() {          // 开始识别  
  47.                 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  48.                 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
  49.                                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  50.                 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");  
  51.                 startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  52.                 // 调出识别界面  
  53.     }  
  54.    
  55.        @Override  
  56.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  57.                 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {  
  58.                          // Fill the list view with the strings the recognizer thought it could have heard  
  59.                          ArrayList matches = data.getStringArrayListExtra(  
  60.                                             RecognizerIntent.EXTRA_RESULTS);  
  61.                          mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,  
  62.                                             matches));  
  63.                 }  
  64.                 // 语音识别后的回调,将识别的字串在list中显示  
  65.                 super.onActivityResult(requestCode, resultCode, data);  
  66.        }  
  67. }  
 
  • android语音识别方法二:应用程序自己调用语音识别库

1.
说明
以下例程功能为:应用程序自身调用语言识别函数,程序以循环方式等待录音并识别后的字串。
2.
本例参考自android代码:
frameworks/base/core/java/android/speech/srec/Recognizer.java中注释部分
3.
可从此处下载可独立运行的代码:代码在一楼
4.
核心代码及说明

Java代码   收藏代码
  1. package com.android.mystt2;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10.    
  11. import android.speech.srec.Recognizer;  
  12. import android.speech.srec.MicrophoneInputStream;  
  13. import java.io.InputStream;  
  14. import java.io.IOException;  
  15. import android.util.Log;  
  16.    
  17. public class MyStt2Activity extends Activity implements OnClickListener {  
  18.        private TextView mText;  
  19.        private static final String TAG = "MyStt3Activity";  
  20.    
  21.        @Override  
  22.        public void onCreate(Bundle savedInstanceState) {  
  23.                 super.onCreate(savedInstanceState);  
  24.                 setContentView(R.layout.main);  
  25.                 Button speakButton = (Button) findViewById(R.id.btn_speak);     // 识别扭按  
  26.                 mText = (TextView) findViewById(R.id.text);      // 显示识别后的字串  
  27.                 speakButton.setOnClickListener(this);  
  28.        }  
  29.    
  30.        public void onClick(View v) {  
  31.                 if (v.getId() == R.id.btn_speak) {  
  32.                          test();  
  33.                 }  
  34.        }  
  35.    
  36.        void test() {  
  37.                 try {  
  38.                          InputStream audio = new MicrophoneInputStream(1102511025 * 5); //设置输入参数  
  39.                          String cdir = Recognizer.getConfigDir(null);    // 获取语音识别配置目录  
  40.                          Recognizer recognizer = new Recognizer(cdir + "/baseline11k.par");  
  41.                          Recognizer.Grammar grammar = recognizer.new Grammar(cdir  
  42.                                             + "/grammars/VoiceDialer.g2g");  
  43.                          grammar.setupRecognizer();  
  44.                          grammar.resetAllSlots();  
  45.                          grammar.compile();  
  46.                          recognizer.start();        // 开始识别  
  47.                          while (true) {       // 循环等待识别结果  
  48.                                    switch (recognizer.advance()) {  
  49.                                    case Recognizer.EVENT_INCOMPLETE:  
  50.                                    case Recognizer.EVENT_STARTED:  
  51.                                    case Recognizer.EVENT_START_OF_VOICING:  
  52.                                    case Recognizer.EVENT_END_OF_VOICING:  
  53.                                             continue;    // 未完成,继续等待识别结果  
  54.                                    case Recognizer.EVENT_RECOGNITION_RESULT:  
  55.                                             for (int i = 0; i < recognizer.getResultCount(); i++) {  
  56.                                                      String result = recognizer.getResult(i,  
  57.                                                                         Recognizer.KEY_LITERAL);  
  58.                                                      Log.d(TAG, "result " + result);  
  59.                                                      mText.setText(result);  
  60.                                             }        // 识别到字串,显示并退出循环  
  61.                                             break;  
  62.                                    case Recognizer.EVENT_NEED_MORE_AUDIO:  
  63.                                             recognizer.putAudio(audio)   // 需要更多音频数据;  
  64.                                             continue;  
  65.                                    default:  
  66.                                             break;  
  67.                                    }  
  68.                                    break;  
  69.                          }  
  70.                          recognizer.stop();  
  71.                          recognizer.destroy();  
  72.                          audio.close();      // 回收资源  
  73.                 } catch (IOException e) {  
  74.                          Log.d(TAG, "error", e);  
  75.                          mText.setText("error " + e);  
  76.                 }  
  77.        }  
  78. }  
 
  • 语音识别方法三:使用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.
核心代码及说明

Java代码   收藏代码
  1. package com.android.mystt3;  
  2.    
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.speech.RecognitionListener;  
  9. import android.speech.RecognizerIntent;  
  10. import android.speech.SpeechRecognizer;  
  11. import android.widget.Button;  
  12. import android.widget.TextView;  
  13. import java.util.ArrayList;  
  14. import android.util.Log;  
  15.    
  16. public class MyStt3Activity extends Activity implements OnClickListener {  
  17.        private TextView mText;  
  18.        private SpeechRecognizer sr;  
  19.        private static final String TAG = "MyStt3Activity";  
  20.    
  21.        @Override  
  22.        public void onCreate(Bundle savedInstanceState) {  
  23.                 super.onCreate(savedInstanceState);  
  24.                 setContentView(R.layout.main);  
  25.                 Button speakButton = (Button) findViewById(R.id.btn_speak);     // 识别按钮  
  26.                 mText = (TextView) findViewById(R.id.text);      // 显示识别字串  
  27.                 speakButton.setOnClickListener(this);  
  28.                 sr = SpeechRecognizer.createSpeechRecognizer(this);        // 初始化识别工具,得到句柄  
  29.                 sr.setRecognitionListener(new listener());         // 注册回调类及函数  
  30.        }  
  31.    
  32.        class listener implements RecognitionListener            // 回调类的实现  
  33.        {  
  34.                 public void onReadyForSpeech(Bundle params)  
  35.                 {  
  36.                          Log.d(TAG, "onReadyForSpeech");  
  37.                 }  
  38.                 public void onBeginningOfSpeech()  
  39.                 {  
  40.                          Log.d(TAG, "onBeginningOfSpeech");  
  41.                 }  
  42.                 public void onRmsChanged(float rmsdB)  
  43.                 {  
  44.                          Log.d(TAG, "onRmsChanged");  
  45.                 }  
  46.                 public void onBufferReceived(byte[] buffer)  
  47.                 {  
  48.                          Log.d(TAG, "onBufferReceived");  
  49.                 }  
  50.                 public void onEndOfSpeech()  
  51.                 {  
  52.                          Log.d(TAG, "onEndofSpeech");  
  53.                 }  
  54.                 public void onError(int error)  
  55.                 {  
  56.                          Log.d(TAG,  "error " +  error);  
  57.                          mText.setText("error " + error);  
  58.                 }  
  59.                 public void onResults(Bundle results)     // 返回识别到的数据  
  60.                 {  
  61.                          String str = new String();  
  62.                          Log.d(TAG, "onResults " + results);  
  63.                          ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);  
  64.                          for (int i = 0; i < data.size(); i++)  
  65.                          {  
  66.                                    Log.d(TAG, "result " + data.get(i));  
  67.                                    str += data.get(i);  
  68.                          }  
  69.                          mText.setText(str);        // 显示被识别的数据  
  70.                 }  
  71.                 public void onPartialResults(Bundle partialResults)  
  72.                 {  
  73.                          Log.d(TAG, "onPartialResults");  
  74.                 }  
  75.                 public void onEvent(int eventType, Bundle params)  
  76.                 {  
  77.                          Log.d(TAG, "onEvent " + eventType);  
  78.                 }  
  79.        }  
  80.    
  81.        public void onClick(View v) {  
  82.                 if (v.getId() == R.id.btn_speak) {  
  83.                          sr.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));  
  84.                 }  
  85.        }  


你可能感兴趣的:(java,android,ListView,service,button,audio)