下面是重要代码,完整DEMO,请在最下面的地址下载。
XML文件
View Code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" > <EditText android:id="@+id/txt_result" android:layout_width="fill_parent" android:layout_height="fill_parent" android:editable="false" android:gravity="top|left" android:paddingBottom="40dip" android:textSize="20sp" /> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt_search" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="搜索" > </Button> <Button android:id="@android:id/button2" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
Activity类
View Code
package com.test.demo; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import com.iflytek.speech.RecognizerResult; import com.iflytek.speech.SpeechConfig.RATE; import com.iflytek.speech.SpeechError; import com.iflytek.ui.RecognizerDialog; import com.iflytek.ui.RecognizerDialogListener; public class HelloDemoActivity extends Activity implements OnClickListener { protected static final String TAG = "ThirdActivity"; private EditText txt_result; private RecognizerDialog rd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo); findView(); //RecognizerDialog(Context context, String params); "appid=1234567,usr=test,pwd=12345" usr、pwd不是必选的 //创建语音识别dailog对象,appid到讯飞就注册获取 rd = new RecognizerDialog(this ,"appid=50e1b967"); } private void findView() { txt_result = (EditText) findViewById(R.id.txt_result); findViewById(R.id.bt_search).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_search: showReconigizerDialog(); break; default: break; } } private void showReconigizerDialog() { //setEngine(String engine,String params,String grammar); /** * 识别引擎选择,目前支持以下五种 “sms”:普通文本转写 “poi”:地名搜索 “vsearch”:热词搜索 “vsearch”:热词搜索 “video”:视频音乐搜索 “asr”:命令词识别 params 引擎参数配置列表 附加参数列表,每项中间以逗号分隔,如在地图搜索时可指定搜索区域:“area=安徽省合肥市”,无附加参数传null */ rd.setEngine("sms", null, null); //设置采样频率,默认是16k,android手机一般只支持8k、16k.为了更好的识别,直接弄成16k即可。 rd.setSampleRate(RATE.rate16k); final StringBuilder sb = new StringBuilder(); Log.i(TAG, "识别准备开始............."); //设置识别后的回调结果 rd.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> result, boolean isLast) { for (RecognizerResult recognizerResult : result) { sb.append(recognizerResult.text); Log.i(TAG, "识别一条结果为::"+recognizerResult.text); } } @Override public void onEnd(SpeechError error) { Log.i(TAG, "识别完成............."); txt_result.setText(sb.toString()); Log.i(TAG, "识别完成:"+txt_result.getText().toString()); } }); //先设置为空,等识别完成后设置内容 txt_result.setText(""); rd.show(); } }
配置文件
View Code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".HelloDemoActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
完整DEMO下载地址:http://download.csdn.net/detail/androidsj/5234326