android 语音识别

您可能听说过“ Google Now项目” ,在这里您可以发出语音命令,Android会为您获取结果。 它可以识别您的声音并将其转换为文本或采取适当的措施。 您有没有想过如何做? 如果您的答案是语音识别API,那么您绝对正确。 最近,在使用Android语音识别API时,我发现了一些有趣的东西。 API真的很容易与应用程序一起使用。 下面给出的是有关语音/语音识别API的小教程。 最终的应用程序看起来类似于下面显示的应用程序。 该应用程序可能不支持Android模拟器,因为它不支持语音识别。 但是同样可以在电话上使用。

项目信息:有关项目的元数据。

平台版本: Android API级别15。IDE: Eclipse Helios服务版本2模拟器: Android 4.1(API 16)

先决条件:对Android应用程序框架和Intent有初步了解。

语音识别功能可以通过RecognizerIntent实现。 创建类型为RecognizerIntent的Intent,并传递额外的参数并开始结果的活动。 基本上,它会启动由您的附加参数定制的识别器提示。 内部语音识别与服务器通信并获取结果。 因此,您必须提供该应用程序的Internet访问权限。 Android Jelly Bean(API级别16)不需要互联网连接即可执行语音识别。 语音识别完成后,识别器将在onActivityResult()方法参数中返回值。

首先通过Eclipse> File> New Project> Android Application Project创建项目 将出现以下对话框。 填写必填字段,即“应用程序名称”,“项目名称”和“包”。 现在按下一步按钮。

出现对话框后,选择BlankActivity,然后单击下一步

在中填写活动名称和布局文件名 如下所示的对话框,然后单击完成按钮。

此过程将设置基本项目文件。 现在,我们将在activity_voice_recognition.xml文件中添加四个按钮。 您可以使用图形布局编辑器或xml编辑器来修改布局文件。 该文件的内容如下所示。 正如你可能会注意到,我们已附上说话()方法中的onClick标签按钮。 单击按钮后,将执行talk()方法。 我们将在主要活动中定义speak()。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><EditTextandroid:id="@+id/etTextHint"android:gravity="top"android:inputType="textMultiLine"android:lines="1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/etSearchHint"/><Buttonandroid:id="@+id/btSpeak"android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="speak"android:padding="@dimen/padding_medium"android:text="@string/btSpeak"tools:context=".VoiceRecognitionActivity" /><Spinnerandroid:id="@+id/sNoOfMatches"android:layout_width="match_parent"android:layout_height="wrap_content"android:entries="@array/saNoOfMatches"android:prompt="@string/sNoOfMatches"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/tvTextMatches"android:textStyle="bold" /><ListViewandroid:id="@+id/lvTextMatches"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

您可能已经注意到,正在从资源访问String常量。 现在,在string.xml中添加字符串常量。 该文件应类似于下面显示的文件。

<resources><string name="app_name">VoiceRecognitionExample</string><string name="btSpeak">Speak</string><string name="menu_settings">Settings</string><string name="title_activity_voice_recognition">Voice Recognition</string><string name="tvTextMatches">Text Matches</string><string name="sNoOfMatches">No of Matches</string><string name="etSearchHint">Speech hint here</string><string-array name="saNoOfMatches"><item>1</item><item>2</item><item>3</item><item>4</item><item>5</item><item>6</item><item>7</item><item>8</item><item>9</item><item>10</item></string-array>
</resources>

现在让我们定义Activity类。 该活动类将在checkVoiceRecognition()方法的帮助下,首先检查语音识别是否可用。 如果语音识别功能不可用,则敬酒一条消息并禁用该按钮。 此处定义了Speak()方法,一旦按下语音按钮便会调用该方法。 在此方法中,我们将创建RecognizerIntent并传递额外的参数。 下面的代码带有嵌入式注释,使注释易于理解。

package com.rakesh.voicerecognitionexample;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.app.SearchManager;
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.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;public class VoiceRecognitionActivity extends Activity {private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;private EditText metTextHint;private ListView mlvTextMatches;private Spinner msTextMatches;private Button mbtSpeak;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_voice_recognition);metTextHint = (EditText) findViewById(R.id.etTextHint);mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);msTextMatches = (Spinner) findViewById(R.id.sNoOfMatches);mbtSpeak = (Button) findViewById(R.id.btSpeak);checkVoiceRecognition()}public void checkVoiceRecognition() {// Check if voice recognition is presentPackageManager pm = getPackageManager();List<resolveinfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);if (activities.size() == 0) {mbtSpeak.setEnabled(false);mbtSpeak.setText("Voice recognizer not present")Toast.makeText(this, "Voice recognizer not present",Toast.LENGTH_SHORT).show();}}public void speak(View view) {Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);// Specify the calling package to identify your applicationintent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());// Display an hint to the user about what he should say.intent.putExtra(RecognizerIntent.EXTRA_PROMPT, metTextHint.getText().toString());// Given an hint to the recognizer about what the user is going to say//There are two form of language model available//1.LANGUAGE_MODEL_WEB_SEARCH : For short phrases//2.LANGUAGE_MODEL_FREE_FORM  : If not sure about the words or phrases and its domain.
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);// If number of Matches is not selected then return show toast messageif (msTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {Toast.makeText(this, "Please select No. of Matches from spinner",Toast.LENGTH_SHORT).show();return;}int noOfMatches = Integer.parseInt(msTextMatches.getSelectedItem().toString());// Specify how many results you want to receive. The results will be// sorted where the first result is the one with higher confidence.intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);//Start the Voice recognizer activity for the result.startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)//If Voice recognition is successful then it returns RESULT_OKif(resultCode == RESULT_OK) {ArrayList<string> textMatchList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);if (!textMatchList.isEmpty()) {// If first Match contains the 'search' word// Then start web search.if (textMatchList.get(0).contains("search")) {String searchQuery = textMatchList.get(0);searchQuery = searchQuery.replace("search","");Intent search = new Intent(Intent.ACTION_WEB_SEARCH);search.putExtra(SearchManager.QUERY, searchQuery);startActivity(search);} else {// populate the MatchesmlvTextMatches.setAdapter(new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,textMatchList));}}//Result code for various error.}else if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){showToastMessage("Audio Error");}else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){showToastMessage("Client Error");}else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){showToastMessage("Network Error");}else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){showToastMessage("No Match");}else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){showToastMessage("Server Error");}super.onActivityResult(requestCode, resultCode, data);}/*** Helper method to show the toast message**/void showToastMessage(String message){Toast.makeText(this, message, Toast.LENGTH_SHORT).show();}
}

这是Android清单文件。 您可以看到,由于语音识别器需要将查询发送到服务器并获取结果,因此已向应用程序提供了INTERNET权限。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.rakesh.voicerecognitionexample"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="15" /><!-- Permissions --><uses-permission android:name="android.permission.INTERNET" /><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name=".VoiceRecognitionActivity"android:label="@string/title_activity_voice_recognition" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
</manifest>

编码完成后,将手机与系统连接,然后在Eclipse IDE上单击“运行”按钮。 Eclipse将安装并启动该应用程序。 您将在设备屏幕上看到以下活动。

在下一个教程中,我们将学习如何使用Android Jelly Bean(API级别16)中引入的新语音识别API以及示例。

如果您对源代码感兴趣,那么可以从github获得它。

参考:我们的JCG合作伙伴Rakesh Cusat在Code4Reference博客上提供了有关Android语音识别的教程。

翻译自: https://www.javacodegeeks.com/2012/08/android-voice-recognition-tutorial.html

android 语音识别

android 语音识别_Android语音识别教程相关推荐

  1. android 气压传感器_Android传感器教程:气压计传感器

    android 气压传感器 我认为最有趣的主题之一是如何在Android中使用Sensor . 如今,我们的智能手机充满了传感器,我们可以用它来控制我们的应用程序. 最常见的传感器是: 全球定位系统 ...

  2. android开发-百度语音识别Android SDK的简单使用

    目录 1.引言 2.开发环境 3.准备开发环境 3.1安装Android studio 3.2创建百度智能云平台应用 3.1下载百度语音识别SDK 4.实现语音识别的简单案例 4.1创建Android ...

  3. 【转】Android 轻松实现语音识别

    2019独角兽企业重金招聘Python工程师标准>>> [转自:老枪] 苹果的iphone 有语音识别用的是Google 的技术,做为Google 力推的Android 自然会将其核 ...

  4. Android华为HiAI语音识别的集成与使用

    一.语音识别简介 语音识别是以语音为研究对象,通过语音信号处理和模式识别让机器自动识别和理解人类口述的语言. 语音识别技术,也被称为自动语音识别ASR,就是让机器通过识别和理解过程把语音信号转变为相应 ...

  5. Android语音识别——谷歌语音识别与百度语音识别

    Android语音识别,简单的理解就是把语音转化为文字. 在日常中,语音识别,车载导航.语音输入等,虽然不一定准确,但用途广泛. 这里就介绍下谷歌原生的语音识别与百度的语音识别 谷歌语音识别 谷歌语音 ...

  6. Android开发之语音识别

    Android开发之语音识别 开发背景 RecognizerIntent相关知识 代码解释 完整代码 项目运行及问题解决 开发背景 最近了解了一下Android Q(安卓10),得知Android Q ...

  7. Android集成百度语音识别到HelloWorld需要注意什么?(保姆级教学)

    Android集成百度语音识别怎么避坑? 首先先放一张集成失败的图(记得一定要用真机,因为它不支持VAD,我这里使用Pixel2): 首先你去百度搜索"百度语音识别",或者点击我下 ...

  8. android教程视频教程_Android拖放教程

    android教程视频教程 这篇文章将介绍如何在Android应用程序中实现拖放. (我目前使用的是sdk的4.0版,但最初是使用3.1版编写的. 为什么要使用拖放 当我开始使用涉及在国际象棋棋盘上移 ...

  9. js语音识别_js 语音识别_js 语音识别库 - 云+社区 - 腾讯云

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. 录音文件识别请求,数据结构,android sdk,ios sdk,自学 ...

最新文章

  1. Storm和MR及Spark Streaming的区别
  2. 13-linux定时任务不起作用到的问题解决办法
  3. 性能提升约 7 倍!Apache Flink 与 Apache Hive 的集成
  4. api zabbix 拓扑图 获取_Zabbix报表系统
  5. Kaggle Titanic补充篇
  6. android surfaceview 大小_Android 使用Camera2 API采集视频数据
  7. 05.序列模型 W1.循环序列模型
  8. 3-6:常见任务和主要工具之正则表达式
  9. 一题多解(八)—— 矩阵上三角(下三角)的访问
  10. 小米笔记本解决风扇异响
  11. 解决 IIS 部署网站引用 woff/woff2/svg 字体报 404 错误
  12. 关于英语写作和阅读的学习——施一公教授的两篇博文
  13. ruoyi框架默认的导出Excel功能代码简析
  14. 最好的投资是投资自己:20本投资书单推荐
  15. i2c-test工具说明文档
  16. 【Flutter从入门到实战】⑪、豆瓣案例-1、星星评分Widget、虚线Widget、TabbarWidget、BottomNavigationBarItem的封装、初始化配置抽取
  17. MineCraft - 创世神插件
  18. android 输入法弹出 标题栏不被顶出去
  19. Samba服务器搭建,win10拒绝访问解决方法
  20. 微信小程序富文本渲染(rich-text)换行失效

热门文章

  1. dr.oracle素颜霜好用吗,这3款让韩国代购们月入上万的“素颜霜”真的好用吗?...
  2. Focal loss 中两个加权参数的原理和产生的影响
  3. 地球坐标、 火星坐标、百度坐标转换
  4. java手机号加密概述
  5. Milogs正式发布工作日志管理软件
  6. html中调整lable位置右移,[转载]label标签右对齐
  7. word里deta怎么打
  8. PHP 使用 phpmailer 发送电子邮件
  9. 各大网站 http server分析
  10. 用matlab代码实现QDA,matlab数据库