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. 使用windos模拟搭建web集群(二)

    一.通过rsync搭建备份服务器 这三个目录我们需要做实时热备,他们分别是  系统的脚本目录  系统的配置文件目录  系统的定时任务目录 [root@mage-monitor- ~]# cat /se ...

  2. postman的使用(转载)

    Postman介绍 Postman是google开发的一款功能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件.其主要功能包括: 模拟各种HTTP requests 从常用的 ...

  3. @Html.Raw()用法

    @Html.Raw() 方法输出带有html标签的字符串, 如:@Html.Raw("<div style='color:red'>输出字符串</div>" ...

  4. 腾讯云通信WebIM事件回调的坑~

    最近在开过工作中用到了腾讯IM的功能,由于业务的需要主要使用到了: 1.loginInfo 用户登录,用户信息 2.getRecentContactList 获得最近联系人 3.getLastGrou ...

  5. Razor 常用方法

    结合wdate使用 //WdatePicker控件时间限制 @Html.TextBoxFor(model => model.BookingStart, new { @onClick = &quo ...

  6. 在C#中将金额转换成中文大写金额

    具体代码如下: /// <summary> /// 金额转换成中文大写金额 /// </summary> /// <param name="LowerMoney ...

  7. HDU 1045 Fire Net(搜索剪枝)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045 ...

  8. 版本控制[0] -> git -> 使用 git 进行版本控制

    版本控制 / Version Control 目录 git-版本控制 / git-Version Control git-常用命令 / git-Freq Command git-文件忽略 / git- ...

  9. Cookie和Session在Node.JS中的实践(一)

    Cookie和Session在Node.JS中的实践(一) Cookie和Session是一个非常有趣的概念,也是一个老生常谈的话题.然而,作者看了许多文章,也翻看了几本书籍,它们对Cookie和Se ...

  10. 手把手教你AndroidStudio多渠道打包

    最近不断有朋友向我咨询AndroidStudio多渠道的打包方法,今天整理一下之前积累的打包套路,写一篇文章,手把手的教给大家.        说到多渠道,这里不得不提一下友盟统计,友盟统计是大家日常 ...