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,不过经过大多开发者的真机测 ...
随机推荐
- [Python爬虫]煎蛋网OOXX妹子图爬虫(1)——解密图片地址
之前在鱼C论坛的时候,看到很多人都在用Python写爬虫爬煎蛋网的妹子图,当时我也写过,爬了很多的妹子图片.后来煎蛋网把妹子图的网页改进了,对图片的地址进行了加密,所以论坛里面的人经常有人问怎么请求的 ...
- 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 ...
- 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 ...
- 用于Web开发的8 个最好的跨平台编辑器
1) Best Cross Platform IDE - Brackets Brackets是一个在前端Web开发和设计人员中最流行的开放源码IDE/代码编辑器之一.它拥有一些实用工具能够将HTML ...
- Java基础(十):封装
在面向对象程式设计方法中,封装(英语:Encapsulation)是指一种将抽象性函式接口的实现细节部份包装.隐藏起来的方法.封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访 ...
- Android -- VelocityTracker
VelocityTracker 主要应用于touch event, VelocityTracker通过跟踪一连串事件实时计算出当前的速度. 方法 //获取一个VelocityTracker对象, 用完 ...
- 【Word】Word 2010 设置边框底纹,粘贴漂亮的代码
参考资料: http://jingyan.baidu.com/article/48206aea1a3401216bd6b310.html http://wenku.baidu.com/link?url ...
- C++类型转换的经典例子
- mondrian4 kylin saiku 整合踩坑记录
1 先说了版本: Mondrian 4 .kylin2.2 .saiku 3.15 2 saiku 3.15 使用的xml是基于 mondrian4 的schema的xml.判断是不是mondrian ...
- [android错误] android-support-v*.jar包出现错误。
可以去你安装的sdk目录中获得.\android_sdks\extras\android\support中获得各个jar包: android-support-v4.jar android-suppor ...