android上实现语音识别,基于google的语音识的简单例子.
2014-03-17 15:21
645 查看
语音识别在android上使用起来很方便也很简单.
但是有个前提条件,就是android机器上必须预先安装google的语音搜索工具
语音识别技术是在Android SDK1.5中才加入的(RecognizerIntent),这里我们简单的分析一下自带的api例子,其实它就是通过一个Intent的Action动作来完成的。主要有以下两种模式:
ACTION_RECOGNIZE_SPEECH:一般语音识别,在这种模式下我们可以捕捉到语音的处理后的文字列。
ACTION_WEB_SEARCH:网络搜索
该例子同样是使用ACTION_RECOGNIZE_SPEECH模式,我们需要实现onActivityResult方法,当语音识别结束之后的回调函数。
下面是核心代码:
[java] view
plaincopyprint?
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就能正常打开了.
但是有个前提条件,就是android机器上必须预先安装google的语音搜索工具
语音识别技术是在Android SDK1.5中才加入的(RecognizerIntent),这里我们简单的分析一下自带的api例子,其实它就是通过一个Intent的Action动作来完成的。主要有以下两种模式:
ACTION_RECOGNIZE_SPEECH:一般语音识别,在这种模式下我们可以捕捉到语音的处理后的文字列。
ACTION_WEB_SEARCH:网络搜索
该例子同样是使用ACTION_RECOGNIZE_SPEECH模式,我们需要实现onActivityResult方法,当语音识别结束之后的回调函数。
下面是核心代码:
[java] view
plaincopyprint?
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就能正常打开了.
相关文章推荐
- Android基于google ZXing的简化版BarCodeTest实现二维码的扫描及简单的网页跳转
- 最简单的基于FFmpeg的移动端例子:Android 视频解码器
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android基于google-play-services-vision:8.1.0实现动态人脸检测
- 使用Android-studio简单实现一个NDK的例子
- Android利用RecognizerIntent识别语音并简单实现打电话动作
- Android例子—AIDL实现两个进程间的简单通信
- 最简单的基于FFmpeg的移动端例子:Android 推流器
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- 最简单的基于FFmpeg的移动端例子附件:SDL Android HelloWorld
- 基于DHT11温湿度传感器的Android硬件访问服务的简单实现(二)
- Android HAL实现的三种方式(1) - 基于JNI的简单HAL设计 推荐
- Android下通过pocketsphinx实现离线语音识别的环境搭建和demo运行(续)--实现Windows下的语音识别
- 最简单的基于FFmpeg的移动端例子:Android 视频转码器
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android中基于HTTP的通信技术(5)Google开源库 使用 Volley 实现 JSON 字符串请求
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Android中实现Runnable接口简单例子