Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常ActivityNotFoundExceptio n,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问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="输入搜索关键字" />

    <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"/>权限,因为需要连接网络才可以进行语音识别


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐