Android 语音识别+语音搜索源码 Voice Search
http://blog.csdn.net/u014051380/article/details/21114389
1、判断是否已安装语音搜索功能
- package com.voice.search;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.net.Uri;
- import android.os.Environment;
- import android.speech.RecognizerIntent;
- import android.widget.Toast;
- public class VoiceSearchUtil {
- private static Context context;
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 1;
- public static void getVoiceSearch(Context c) {
- context = c;
- PackageManager pm = context.getPackageManager();
- List<ResolveInfo> activities = pm
- .queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
- if (activities.size() != 0) {
- /* 设备存在 */
- voiceSearch();
- } else {
- /* 没有设备请点击安装按钮进行安装呢 */
- if (copyApkFromAssets(context, "google_voice_search.apk", Environment.getExternalStorageDirectory()
- .getAbsolutePath() + "/google_voice_search.apk")) {
- new AlertDialog.Builder(context).setMessage("检测到未安装语音搜索设备,是否安装?")
- .setPositiveButton("安装", new android.content.DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setDataAndType(
- Uri.parse("file://"
- + Environment.getExternalStorageDirectory().getAbsolutePath()
- + "/google_voice_search.apk"),
- "application/vnd.android.package-archive");
- context.startActivity(intent);
- }
- }).setNegativeButton("取消", null).show();
- }
- }
- }
- /**
- * 安装语音搜索功能,写入数据
- *
- * @param context
- * 上下文
- * @param fileName
- * 文件名称
- * @param path
- * 文件路径
- * @return 所需文件
- */
- public static boolean copyApkFromAssets(Context context, String fileName, String path) {
- boolean copyIsFinish = false;
- try {
- InputStream is = context.getAssets().open(fileName);
- File file = new File(path);
- file.createNewFile();
- FileOutputStream fos = new FileOutputStream(file);
- byte[] temp = new byte[1024];
- int i = 0;
- while ((i = is.read(temp)) > 0) {
- fos.write(temp, 0, i);
- }
- fos.close();
- is.close();
- copyIsFinish = true;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return copyIsFinish;
- }
- // 语音搜索
- private static void voiceSearch() {
- try {
- // 通过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, "开始语音");
- // 开始语音识别
- ((Activity) context).startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(context, "找不到语音设备", Toast.LENGTH_LONG).show();
- }
- }
- }
package com.voice.search;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Environment;
import android.speech.RecognizerIntent;
import android.widget.Toast;
public class VoiceSearchUtil {
private static Context context;
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1;
public static void getVoiceSearch(Context c) {
context = c;
PackageManager pm = context.getPackageManager();
List<ResolveInfo> activities = pm
.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
/* 设备存在 */
voiceSearch();
} else {
/* 没有设备请点击安装按钮进行安装呢 */
if (copyApkFromAssets(context, "google_voice_search.apk", Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/google_voice_search.apk")) {
new AlertDialog.Builder(context).setMessage("检测到未安装语音搜索设备,是否安装?")
.setPositiveButton("安装", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(
Uri.parse("file://"
+ Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/google_voice_search.apk"),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
}).setNegativeButton("取消", null).show();
}
}
}
/**
* 安装语音搜索功能,写入数据
*
* @param context
* 上下文
* @param fileName
* 文件名称
* @param path
* 文件路径
* @return 所需文件
*/
public static boolean copyApkFromAssets(Context context, String fileName, String path) {
boolean copyIsFinish = false;
try {
InputStream is = context.getAssets().open(fileName);
File file = new File(path);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
fos.close();
is.close();
copyIsFinish = true;
} catch (IOException e) {
e.printStackTrace();
}
return copyIsFinish;
}
// 语音搜索
private static void voiceSearch() {
try {
// 通过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, "开始语音");
// 开始语音识别
((Activity) context).startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "找不到语音设备", Toast.LENGTH_LONG).show();
}
}
}
2、assets提供语音搜索apk安装
- package com.voice.search;
- import java.util.ArrayList;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.view.View;
- import android.widget.Toast;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- public class MainActivity extends Activity {
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 1;
- private String[] str;
- private Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- context = this;
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.search_iv:
- VoiceSearchUtil.getVoiceSearch(context);
- break;
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // 回调获取从谷歌得到的数据
- if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
- // 取得语音的字符
- ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- // 循环所有结果存入数组
- str = new String[results.size()];
- for (int i = 0; i < results.size(); i++) { // 循环所有结果
- str[i] = results.get(i);
- }
- // Dialog显示
- getSearchDialog();
- // 这里只获取第一条数据
- Toast.makeText(this, results.get(0), Toast.LENGTH_LONG).show();
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- /***
- * 搜索后dialog显示结果
- */
- private void getSearchDialog() {
- Dialog dialog = new AlertDialog.Builder(this).setTitle("你是不是想找:").setIcon(R.drawable.ic_launcher)
- .setItems(str, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show();
- }
- }).setNegativeButton("取消", null).create();
- dialog.show();
- }
- }
package com.voice.search;
import java.util.ArrayList;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
public class MainActivity extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1;
private String[] str;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.search_iv:
VoiceSearchUtil.getVoiceSearch(context);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 回调获取从谷歌得到的数据
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// 取得语音的字符
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// 循环所有结果存入数组
str = new String[results.size()];
for (int i = 0; i < results.size(); i++) { // 循环所有结果
str[i] = results.get(i);
}
// Dialog显示
getSearchDialog();
// 这里只获取第一条数据
Toast.makeText(this, results.get(0), Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
/***
* 搜索后dialog显示结果
*/
private void getSearchDialog() {
Dialog dialog = new AlertDialog.Builder(this).setTitle("你是不是想找:").setIcon(R.drawable.ic_launcher)
.setItems(str, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, str[which], Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null).create();
dialog.show();
}
}
3、加入Android读写文件权限
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Android 语音识别+语音搜索源码 Voice Search相关推荐
- Android上百实例源码分析以及开源分析集合打包
感谢网友banketree的收集,压缩包的内容如下: 1.360新版特性界面源代码 实现了360新版特性界面的效果,主要涉及到Qt的一些事件处理与自定义控件.但源码好像是c++. 2.aidl跨进程调 ...
- 【转】Ubuntu 14.04.3上配置并成功编译Android 6.0 r1源码
http://www.linuxidc.com/Linux/2016-01/127292.htm 终于成功把Android 6.0 r1源码的源码编译.先上图,这是在Ubuntu中运行的Android ...
- cocos creator麻将教程系列(八)—— 达达麻将语音聊天源码分析
达达麻将语音聊天源码分析 达达麻将版图 语音聊天 1:语音聊天只支持Native平台,iOS与android; 2: 语音聊天的音频格式为amr; 3: native平台实现了语音的录制和播放,可以移 ...
- Android 11.0 Settings源码分析 - 主界面加载
Android 11.0 Settings源码分析 - 主界面加载 本篇主要记录AndroidR Settings源码主界面加载流程,方便后续工作调试其流程. Settings代码路径: packag ...
- 影视全搜索-源码分享-站点采集
影视全搜索源码分享,影视全搜索是一个搜索全网影视资源的站点 基于框架eyz优化而成的影视全搜索是一个集合众多资源站的影视检索程序, 实现全网m3u8影视资源检索.直播源整理等 演示 演示站点:️影 ...
- Android AOSP基础(四)Source Insight和Android Studio导入系统源码
本文首发于微信公众号「刘望舒」 关联系列 Android AOSP基础系列 Android系统启动系列 前言 在上一篇文章Android AOSP基础(三)Android系统源码的整编和单编中,我们对 ...
- android 修改编译内核源码 对抗反调试
0×00 写在前面 攻防对立.程序调试与反调试之间的对抗是一个永恒的主题.在安卓逆向工程实践中,通过修改和编译安卓内核源码来对抗反调试是一种常见的方法.但网上关于此类的资料比较少,且都是基于AOSP ...
- Android contacts 联系人 通讯录 源码 完全解析
Android contacts 联系人 通讯录 源码 完全解析 1简介 2软件架构 3各功能模块分析 1联系人数据的显示 1联系人列表显示 2联系人详细信息数据的显示 2联系人数据的编辑和存储 1编 ...
- Android实用应用程序源码
andriod闹钟源代码 http://www.apkbus.com/android-20974-1-1.html android源码分享之指南针程序 http://www.apkbus.com/an ...
最新文章
- c语言精品课程网站论文免费下载,【毕业论文_c语言程序设计精品课程网站的研究与实现6喜欢就下吧材料】...
- 接口请求,上传byte数组byte[]数据异常,负数变正数/负数变63
- 社区儿童计算机活动总结,社区亲子活动总结
- UVALive 6508 Permutation Graphs
- google 插件_google这4款插件我每天都用,省时无数
- 一组关于女生的秘密数据!
- PHP自动压缩CSS文件方法,php 压缩多个CSS文件的实现代码
- Mac环境下安装配置Redis
- 斗地主Java课程设计_Java课程设计---web版斗地主
- Android App 免杀教程
- 《数据挖掘:R语言实战》P234中,UCI数据库中白酒品质研究数据集4898个样本下载问题
- PostgreSQL 10.1 手册_部分 I. 教程
- docker查看mysql日志_如何查看docker运行日志
- 深入理解audio 系统
- 高校wifi认证登录
- 【视频】极值理论EVT与R语言应用:GPD模型火灾损失分布分析
- caffe accuracy 学习
- Android Studio代码迁移问题小汇总
- Hbuilder 笔记
- 软件技术专业就业方向分析