http://www.apkbus.com/forum.php?mod=viewthread&tid=3473

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

1.

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

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

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

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

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

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

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

android语音识别方法的更多相关文章

  1. Android 通过调用系统,如接口 谷歌语音、百度语音、科大讯飞语音等语音识别方法对话框

    现在app在发展过程中会集成一些语音识别功能,不具有其自己的显影剂一般正在开发的语音识别引擎,所以在大多数情况下,它是选择一个成熟的语音识别引擎SDK集成到他们的app在. 平时,这种整合被分成两个, ...

  2. Android Stuido 方法参数 p0,p1

    Android Stuido 方法参数 p0,p1 参考文献 https://stackoverflow.com/questions/49219439/incorrect-variable-names ...

  3. 蓝牙ble数据转语音实现Android AudioRecord方法推荐

    蓝牙ble数据转语音实现Android AudioRecord方法推荐 教程  欢迎走进zozo的学习之旅. 概述 蓝牙BLE又称bluetooth smart,主打的是低功耗和快速链接,所以在支持的 ...

  4. android 语音识别

    Android中主要通过RecognizerIntent来实现语音识别,事实上代码比較简单.可是假设找不到设置,就会抛出异常ActivityNotFoundException.所以我们须要捕捉这个异常 ...

  5. uni-app&H5&Android混合开发三 || uni-app调用Android原生方法的三种方式

    前言: 关于H5的调用Android原生方法的方式有很多,在该片文章中我主要简单介绍三种与Android原生方法交互的方式. 一.H5+方法调用android原生方法 H5+ Android开发规范官 ...

  6. 实现Android语音识别服务接口 RecognitionService的方法

    之前的一篇文章介绍过SpeechRecognizer类,该类能够作为对外的一个接口,并通过Intent传递一个ComponentName获取可支持语音识别的功能的服务,一般的用户手机中假设安装了语音识 ...

  7. 百度Android语音识别SDK语义理解与解析方法

    百度语义理解开放平台面向互联网开发人员提供自然语言文本的解析服务,也就是能够依据文本的意图解析成对应的表示. 为了易于人阅读,同一时候也方便机器解析和生成,意图表示协议採用 json 语言进行描写叙述 ...

  8. Android语音识别(本地+第三方)

    语音识别主要的功能就是在用户不方便输入的时候找一个替代输入的选择. 1.本地语音识别 下面的代码首先创建SpeechRecognizer对象,并设置回调函数监听器.当在点击监听器中调用doSpeech ...

  9. <交流贴>android语音识别之科大讯飞语音API的使用

      因为最近在研究语音识别,所以借鉴了一下CreAmazing网友的帖子 Android系统本身其实提供有语音识别模块,在它的APIDemo里也有关于语音识别的sample,不过经过大多开发者的真机测 ...

随机推荐

  1. [Python爬虫]煎蛋网OOXX妹子图爬虫(1)——解密图片地址

    之前在鱼C论坛的时候,看到很多人都在用Python写爬虫爬煎蛋网的妹子图,当时我也写过,爬了很多的妹子图片.后来煎蛋网把妹子图的网页改进了,对图片的地址进行了加密,所以论坛里面的人经常有人问怎么请求的 ...

  2. Reverse Nodes in k-Group leetcode java

    题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...

  3. Flatten Binary Tree to Linked List leetcode java

    题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...

  4. 用于Web开发的8 个最好的跨平台编辑器

    1) Best Cross Platform IDE - Brackets Brackets是一个在前端Web开发和设计人员中最流行的开放源码IDE/代码编辑器之一.它拥有一些实用工具能够将HTML ...

  5. Java基础(十):封装

    在面向对象程式设计方法中,封装(英语:Encapsulation)是指一种将抽象性函式接口的实现细节部份包装.隐藏起来的方法.封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访 ...

  6. Android -- VelocityTracker

    VelocityTracker 主要应用于touch event, VelocityTracker通过跟踪一连串事件实时计算出当前的速度. 方法 //获取一个VelocityTracker对象, 用完 ...

  7. 【Word】Word 2010 设置边框底纹,粘贴漂亮的代码

    参考资料: http://jingyan.baidu.com/article/48206aea1a3401216bd6b310.html http://wenku.baidu.com/link?url ...

  8. C++类型转换的经典例子

  9. mondrian4 kylin saiku 整合踩坑记录

    1 先说了版本: Mondrian 4 .kylin2.2 .saiku 3.15 2 saiku 3.15 使用的xml是基于 mondrian4 的schema的xml.判断是不是mondrian ...

  10. [android错误] android-support-v*.jar包出现错误。

    可以去你安装的sdk目录中获得.\android_sdks\extras\android\support中获得各个jar包: android-support-v4.jar android-suppor ...