我们只需要通过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);
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>
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"/>权限,因为需要连接网络才可以进行语音识别
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/jssyy123/article/details/47320591