android语音识别方法
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.
核心代码及说明
- 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);
- }
- }
- android语音识别方法二:应用程序自己调用语音识别库
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);
- }
- }
- }
- 语音识别方法三:使用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.
核心代码及说明
- 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));
- }
- }
- }
android语音识别方法的更多相关文章
- Android 通过调用系统,如接口 谷歌语音、百度语音、科大讯飞语音等语音识别方法对话框
现在app在发展过程中会集成一些语音识别功能,不具有其自己的显影剂一般正在开发的语音识别引擎,所以在大多数情况下,它是选择一个成熟的语音识别引擎SDK集成到他们的app在. 平时,这种整合被分成两个, ...
- Android Stuido 方法参数 p0,p1
Android Stuido 方法参数 p0,p1 参考文献 https://stackoverflow.com/questions/49219439/incorrect-variable-names ...
- 蓝牙ble数据转语音实现Android AudioRecord方法推荐
蓝牙ble数据转语音实现Android AudioRecord方法推荐 教程 欢迎走进zozo的学习之旅. 概述 蓝牙BLE又称bluetooth smart,主打的是低功耗和快速链接,所以在支持的 ...
- android 语音识别
Android中主要通过RecognizerIntent来实现语音识别,事实上代码比較简单.可是假设找不到设置,就会抛出异常ActivityNotFoundException.所以我们须要捕捉这个异常 ...
- uni-app&H5&Android混合开发三 || uni-app调用Android原生方法的三种方式
前言: 关于H5的调用Android原生方法的方式有很多,在该片文章中我主要简单介绍三种与Android原生方法交互的方式. 一.H5+方法调用android原生方法 H5+ Android开发规范官 ...
- 实现Android语音识别服务接口 RecognitionService的方法
之前的一篇文章介绍过SpeechRecognizer类,该类能够作为对外的一个接口,并通过Intent传递一个ComponentName获取可支持语音识别的功能的服务,一般的用户手机中假设安装了语音识 ...
- 百度Android语音识别SDK语义理解与解析方法
百度语义理解开放平台面向互联网开发人员提供自然语言文本的解析服务,也就是能够依据文本的意图解析成对应的表示. 为了易于人阅读,同一时候也方便机器解析和生成,意图表示协议採用 json 语言进行描写叙述 ...
- Android语音识别(本地+第三方)
语音识别主要的功能就是在用户不方便输入的时候找一个替代输入的选择. 1.本地语音识别 下面的代码首先创建SpeechRecognizer对象,并设置回调函数监听器.当在点击监听器中调用doSpeech ...
- <交流贴>android语音识别之科大讯飞语音API的使用
因为最近在研究语音识别,所以借鉴了一下CreAmazing网友的帖子 Android系统本身其实提供有语音识别模块,在它的APIDemo里也有关于语音识别的sample,不过经过大多开发者的真机测 ...
随机推荐
- Median of Two Sorted Array leetcode java
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- Gradle for Android 翻译 -1
英文版电子书下载 参考:Gradle for Android 一.从 Gradle 和 AS 开始 [Getting Started with Gradle and Android Studio] ...
- 7 个 Bootstrap 在线编辑器用于快速开发响应式网站
Bootstrap 已经使响应式网站开发变得简单很多. 但是如果你不必手动写全部代码,事情会如何呢? 如果你可以自由地选择你想要使用的Bootstrap 组件.并可以把它们拖拽到画布中,事情会如何呢? ...
- 使用Spring提供Quartz来实现定时任务
Spring功能越来越多了,用起来还很舒服方便,Quartz实现的定时任务就是一个. 首先是配置文件: <?xml version="1.0" encoding=" ...
- 免费资源:JellyFish的iOS8应用图标集
本地下载 包含设计和PNG效果图片的iOS8的图标集合.
- 如何为Android上的产品设计一款合适的图标
如 果你已经完成了你的app,你一定会马上向其它人宣布这件事情.但是你需要注意一个很重要的问题,那就是app的图标.你的图标可能在项目启动之 前就已经设计好了,但我不喜欢这样,如果app没有完成实际上 ...
- listView/GridView getChild获取不到的解决方法
在onCreate或onResume中调用了getChildAt()方法,这时候adapter中的Item还没有放入到AdapterView中去.... 解决方法,当activity获得焦点事件的时候 ...
- 搭建一个SpringBoot项目
1.创建项目 New->Spring Starter Project 2.添加支持 增加对mybatis plus的支持,修改pom.xml,增加如下内容: <dependency> ...
- Nginx启用ssl以及免费证书申请
主要是这个东西,折腾了我两天,所以记录下来. 最开始是在meteor下面调用一个webservice,但是发现meteor项目的发布环境时https,所以请求的webservice也必须时webser ...
- Hibernate(四)结构-基础语义和事务
一.基础语义 核心: Configuration SessionFactory Session 二.Configuration Configuration类负责管理Hibernate的配置信息,Hib ...