android 语音识别

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

RecognizerIntent的一些常量:

android 语音识别

我们仅仅须要通过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 语音识别

以下我们进入android语音识别编程中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"/>权限,由于须要连接网络才干够进行语音识别