android 语音识别
来源:互联网 发布:金盏花洁面啫喱知乎 编辑:程序博客网 时间:2024/06/04 22:59
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="输入搜索关键字" /> <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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mbtnSpeech = (Button)findViewById(R.id.btn_speech);meditTex = (EditText)findViewById(R.id.edittext);mbtnSpeech.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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 @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubif (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"/>权限,因为需要连接网络才可以进行语音识别
1 0
- android 语音识别接口
- android语音识别简介
- Android语音识别
- android语音识别代码
- android 本地语音识别
- android 语音识别
- android 语音识别
- Android-语音识别
- Android 语音识别
- Android 实现语音识别
- android 语音识别
- android语音识别demo
- android 语音识别
- android语音识别技术
- Android实现语音识别
- Android语音识别
- Android语音识别
- android 语音识别接口
- 通知
- aws 代码扫描所有dynamoDB数据返回ScanItemResult格式
- UVALive - 4043 Ants (KM裸题)
- 手机号码匹配
- 杭电 1016 Prime Ring Problem【DFS】
- android 语音识别
- Java--内部类,局部类与匿名类
- linux上寻找并杀死僵尸进程
- 黑马程序员——Java Collection集合
- tiff文件读取若干问题
- pom.xml中配置plugin补充
- 学习网站博客
- iOS即时通讯
- eclipse tomcat May be locked by another process 解决