Android中主要通过RecognizerIntent来实现语音识别,事实上代码比較简单。可是假设找不到设置,就会抛出异常ActivityNotFoundException。所以我们须要捕捉这个异常。并且语音识别在模拟器上是无法測试的。由于语音识别是訪问google云端数据,所以假设手机的网络没有开启。就无法实现识别声音的!一定要开启手机的网络,假设手机不存在语音识别功能的话。也是无法启用识别。

RecognizerIntent的一些常量:

我们仅仅须要通过Intent来传递一个动作以及一些属性。然后通过startActivityForResult来開始语音。代码例如以下:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"開始语音,请讲话。

。

。。"); startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

先上图,无图不编程



以下我们进入android语音识别编程中

1、创建简单的布局activity_main.xml

<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <EditText
android:id="@+id/edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="输入搜索keyword" /> <Button
android:id="@+id/btn_speech"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="语音识别"
android:layout_weight="1"/> </LinearLayout></span>

2、创建活动 MainActivity.java

package com.example.speechdetectortest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private Button mbtnSpeech;
private EditText meditTex;
private int VOICE_RECOGNITION_REQUEST_CODE = 10000;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mbtnSpeech = (Button)findViewById(R.id.btn_speech);
meditTex = (EditText)findViewById(R.id.edittext); mbtnSpeech.setOnClickListener(this);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_speech:
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, "開始语音,请讲话。。。 。。");
//開始语音识别
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// TODO: handle exception
//找不到语音设备装置
Toast.makeText(this, " 找不到语音设备装置 ", Toast.LENGTH_LONG).show();
// startActivity(new Intent("android.intent.action.VIEW", Uri.parse("market://search?q=pname:com.google.android.voicesearch")));
} break; default:
break;
}
} //当语音结束时的回调函数onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// 取得语音的字符
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (results != null) {
//设置视图更新
String strText = results.get(0);
meditTex.setText(strText);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

3、最后不要忘记加上 <uses-permission android:name="android.permission.INTERNET"/>权限,由于须要连接网络才干够进行语音识别

android 语音识别的更多相关文章

  1. android语音识别方法

    http://www.apkbus.com/forum.php?mod=viewthread&tid=3473 android语音识别方法一:使用intent调用语音识别程序 1. 说明 以下 ...

  2. Android语音识别(本地+第三方)

    语音识别主要的功能就是在用户不方便输入的时候找一个替代输入的选择. 1.本地语音识别 下面的代码首先创建SpeechRecognizer对象,并设置回调函数监听器.当在点击监听器中调用doSpeech ...

  3. Android语音识别--->>RecongnizerIntent实现

    首先,咱得先说下注意点: Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常 ActivityNotFoundException ...

  4. Android语音识别

    语音识别 - 科大讯飞 开放平台 http://open.voicecloud.cn/ 需要拷贝lib.assets.并在清单文件中写一些权限 public class MainActivity ex ...

  5. android语音识别技术

      今天从网上找了个例子实现了语音识别,个人感觉挺好玩的,就把代码贴出来与大家分享下: Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就 ...

  6. <交流贴>android语音识别之科大讯飞语音API的使用

      因为最近在研究语音识别,所以借鉴了一下CreAmazing网友的帖子 Android系统本身其实提供有语音识别模块,在它的APIDemo里也有关于语音识别的sample,不过经过大多开发者的真机测 ...

  7. android语音识别和合成第三方 .

    讯飞语音云 http://open.voicecloud.cn/index.php 目前支持6大类型的SDK下载,包括Android. iPhone平台移动应用的接入,Windows.Linux平台P ...

  8. 实现Android语音识别服务接口 RecognitionService的方法

    之前的一篇文章介绍过SpeechRecognizer类,该类能够作为对外的一个接口,并通过Intent传递一个ComponentName获取可支持语音识别的功能的服务,一般的用户手机中假设安装了语音识 ...

  9. 百度Android语音识别SDK语义理解与解析方法

    百度语义理解开放平台面向互联网开发人员提供自然语言文本的解析服务,也就是能够依据文本的意图解析成对应的表示. 为了易于人阅读,同一时候也方便机器解析和生成,意图表示协议採用 json 语言进行描写叙述 ...

随机推荐

  1. 清明小长假之VUE.JS学习测试码

    我们放了四天假,刚好借此机会,系统的了解一下VUE.JS. <!DOCTYPE html> <html> <head> <meta charset=" ...

  2. 大牛教你如何循序渐进,有效的学习JavaScript?

    首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门.谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我们”.您先别急着骂,先听我说! 你叫一个大学生去教小学数学 ...

  3. 使用webView的时候,出现Error loading page Domain:WebKitErrorDomain Error Code:101 Description: The URL can't be shown

    onShouldStartLoadWithRequest = (e) => {// Implement any custom loading logic here, don't forget t ...

  4. Wannafly挑战赛4 A解方程【二分/set/hash求解方程】

    https://www.nowcoder.com/acm/contest/35/A 题目描述 给出n个整数和x,请问这n个整数中是否存在三个数a,b,c使得ax2+bx+c=0,数字可以重复使用. 输 ...

  5. 模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合)

    模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合) Code: #include <bits/stdc++.h> using namespace std; #define ...

  6. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

  7. response.getWriter().write()与out.print()的区别(转)

    1.首先介绍write()和print()方法的区别: (1).write():仅支持输出字符类型数据,字符.字符数组.字符串等 (2).print():可以将各种类型(包括Object)的数据通过默 ...

  8. MySQL 5.7.17 Group Replication 初始

    http://blog.csdn.net/mchdba/article/details/53957248

  9. jmeter的dubbo插件

    调研是否可以把dubbo压测的一些公共配置变成变量.可以调控 Dubbo接口如何在Jmeter中测试,自研Dubbo Plugin for Apache JMeter 最新使用手册参考:https:/ ...

  10. [ACM] POJ 1035 Spell checker (单词查找,删除替换添加不论什么一个字母)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18693   Accepted: 6844 De ...