Android-调用自带的谷歌语音识别技术(一)
来源:互联网 发布:win8没有windows图标 编辑:程序博客网 时间:2024/06/02 03:57
这篇文章记录下安卓自带的谷歌的语音识别技术。
谷歌作为IT巨头特别是安卓之父,语音搜索这个功能无疑是强大的。
在安卓中实现语音识别的代码很简单,我们要实现的就是调用谷歌api获取搜索结果,然后根据这个结果做自己想要的功能。
Android 中主要通过RecognizerIntent来实现语音识别。先来介绍下RecognizerIntent的常量
看一下调用谷歌之后的效果图
注意:
1.不支持谷歌搜索服务的手机不能使用此功能,比如我的opp手机,用的是三星的测试机,在使用这个功能的时候会检测,是否能够下载
2.此功能需要联网并且手机翻墙,当我使用电脑翻墙之后分享无线网,亲测不可以!这里推荐手机翻墙软件介绍的网址
3.在AndroidManifest中设置网络权限
<uses-permission android:name="android.permission.INTERNET" />
下面是代码
public class MainActivity extends Activity { protected static final int RESULT_SPEECH = 1; private ImageButton btnSpeak; private TextView txtText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtText = (TextView) findViewById(R.id.txtText); btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); btnSpeak.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //开启语音识别功能 Intent intent = new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH); //设置模式,目前设置的是自由识别模式 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); //提示语言开始文字,就是效果图上面的文字 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please start your voice"); 开始识别,这里检测手机是否支持语音识别并且捕获异常 try { startActivityForResult(intent, RESULT_SPEECH); txtText.setText(""); } catch (ActivityNotFoundException a) { Toast t = Toast.makeText(getApplicationContext(), "Opps! Your device doesn't support Speech to Text", Toast.LENGTH_SHORT); t.show(); } } }); } //接收返回的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case RESULT_SPEECH: { if (resultCode == RESULT_OK && data != null) { //返回结果是一个list,我们一般取的是第一个最匹配的结果 ArrayList<String> text = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); txtText.setText(text.get(0)); } break; } } }
下面附上在github上面找的一个小demo
https://github.com/bradleybossard/android-speech-to-text-demo
阅读全文
0 0
- Android-调用自带的谷歌语音识别技术(一)
- C# 调用微软自带的语音识别
- Android 调用谷歌语音识别
- 【Android 调用谷歌语音识别】
- Android 调用谷歌语音识别
- Android 调用谷歌语音识别
- Android 通过调用谷歌语音系统接口弹出语音识别对话框的方法 .
- Android 通过调用谷歌语音系统接口弹出语音识别对话框的方法 .
- Android教程之Android自带的语音识别例子初探
- Android教程之Android自带的语音识别例子初探
- Android教程之Android自带的语音识别例子初探
- android语音识别技术
- android语音识别技术
- android语音识别技术
- android语音识别技术
- Android语音识别技术
- android语音识别技术
- android语音识别技术
- 微信公众号开发之编码问题
- DataTables学习
- Mac下的SVN客户端工具Cornerstone使用教程
- go依赖包管理工具对比
- c++ 将两个有序的单链表AB合并到新的单链表C中
- Android-调用自带的谷歌语音识别技术(一)
- 用 Go 构建一个区块链 -- Part 2: 工作量证明
- 计算机网络学习(一)
- 空间配置器
- sas 数组
- android调整图片大小
- 不同语言基本数据类型(int、char、float、double....)对应字节大小
- 使用CMake来进行Android NDK开发
- String转换为json