//配置文件
这里所需jar包为:armeabi Msc.jar
//布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="您说点的什么。。。。。。。。。" android:layout_marginTop="20dp" /> <Button android:id="@+id/button" android:layout_below="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="按住请说话" /> </RelativeLayout>
//主类
package com.baidu.myvoid; import java.util.ArrayList; import com.iflytek.speech.RecognizerResult; import com.iflytek.speech.SpeechError; import com.iflytek.speech.SpeechConfig.RATE; import com.iflytek.ui.RecognizerDialog; import com.iflytek.ui.RecognizerDialogListener; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private Button button; private EditText content; protected static final String TAG = "ThirdActivity"; private RecognizerDialog rd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); //创建语音识别dailog对象,appid到讯飞就注册获取 rd = new RecognizerDialog(this ,"appid=50e1b967"); } private void init() { //找到相应的控件 button = (Button) findViewById(R.id.button); content = (EditText) findViewById(R.id.content); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //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, "识别完成............."); content.setText(sb.toString()); Log.i(TAG, "识别完成:"+content.getText().toString()); } }); content.setText(""); //先设置为空,等识别完成后设置内容 rd.show(); } }); } }
//所需的权限
<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" />