android上实现语音识别,基于google的语音识的简单例子.
来源:互联网 发布:炉石传说淘宝卡包 编辑:程序博客网 时间:2024/04/20 11:39
语音识别在android上使用起来很方便也很简单.
但是有个前提条件,就是android机器上必须预先安装google的语音搜索工具
语音识别技术是在Android SDK1.5中才加入的(RecognizerIntent),这里我们简单的分析一下自带的api例子,其实它就是通过一个Intent的Action动作来完成的。主要有以下两种模式:
ACTION_RECOGNIZE_SPEECH:一般语音识别,在这种模式下我们可以捕捉到语音的处理后的文字列。
ACTION_WEB_SEARCH:网络搜索
该例子同样是使用ACTION_RECOGNIZE_SPEECH模式,我们需要实现onActivityResult方法,当语音识别结束之后的回调函数。
下面是核心代码:
- package com.ifeisu.test.voice;
- import java.util.ArrayList;
- import java.util.List;
- 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;
- /***
- * 语音识别demo,在使用此demo之前需要确认手机或者模拟器上安装了google的语音搜索工具
- * Voice_Search_2.1.4.apk或者更高版本
- * @author guoxinzz@163.com
- * @website http://www.ifeisu.com
- *
- */
- public class VoiceRecognition extends Activity implements OnClickListener {
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
- private ListView mList;
- /**
- *
- * Called with the activity is first created.
- */
- @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);
- // Check to see if a recognition activity is present
- PackageManager pm = getPackageManager();
- List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
- RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 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)
- {
- startVoiceRecognitionActivity();
- }
- }
- private void startVoiceRecognitionActivity()
- {
- // 通过Intent传递语音识别的模式
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- // 语言模式和自由形式的语音识别
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- // 提示语音开始
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
- "请说出需要打开的网站名字.");
- // 开始执行我们的Intent、语音识别
- startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
- }
- // 当语音结束时的回调函数onActivityResult
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
- && resultCode == RESULT_OK)
- {
- // 取得语音的字符
- ArrayList<String> matches = data
- .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- mList.setAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, matches));
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
1.如果手机不支持语音搜索,则需要下载google的语音搜索工具包 Voice_Search_2.1.4.apk,这个可以到hiapk.com或者其他应用商店下载到.
2.源码下载:demo.voice_recognition.rar 因为csdn不让上传附件,在连接上右键->连接另存为,下载回去将名字改为demo.voice_recognition.rar就能正常打开了.
0 0
- android上实现语音识别,基于google的语音识的简单例子.
- android上实现语音识别,基于google的语音识的简单例子.
- 语音识别的例子
- 关于android上调用google的语音识别功能
- 基于Google的嵌入式系统android开发语音技术(语音搜索,语音朗读文章,语音控制)
- Google的语音识别API
- 一个语音识别的例子
- 另一个语音识别的例子
- 基于科大讯飞的语音识别
- Android语音识别的具体实现实例
- Android 实现语音识别的完整代码
- 基于Tensorflow的VCTK语音识别例子测试
- 【Google Voice】Android 轻松实现语音识别
- kaldi上第一个免费的中文语音识别例子
- kaldi上第一个免费的中文语音识别例子
- kaldi上第一个免费的中文语音识别例子
- 语音识别 一个超简单的语音听写识别编程
- 在Android Studio下使用百度语音识别的一个简单例子
- 为何说你不懂html5开发网页设计
- c++宏定义
- socket阻塞与非阻塞,同步与异步、I/O模型
- XY苹果助手简介
- 一步步学习SPD2010--第十章节--SP网站品牌化(5)--创建具有功能的HTML页面
- android上实现语音识别,基于google的语音识的简单例子.
- google日历,接上显示器,挂到墙上
- Spring与RMI集成实现远程访问
- 第三方支付或再遭重击:余额宝1年购买限额1万
- IOS 学习笔记7-NSSearchPathForDirectoriesInDomains
- linux系统字符集影响TOMCAT运行,造成页面HTML代码输出不全
- match_parent和fill_parent的区别
- java的一些学习网址。。。。。
- WinRT的IBuffer