android语音识别之科大讯飞话音API的使用

来源:互联网 发布:画路线图的软件 编辑:程序博客网 时间:2024/05/09 14:19
android语音识别之科大讯飞语音API的使用
布局文件:

<?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:orientation="vertical" >    <EditText        android:id="@+id/et"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/bt_recognize"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="Recognize" />    <Button        android:id="@+id/bt_speek"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="Speek" />    <Button        android:id="@+id/bt_speek_bg"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="Speek-Background" /></LinearLayout>


Activity代码如下:
package sina.CreAmazing.voice;import java.util.ArrayList;import com.iflytek.speech.RecognizerResult;import com.iflytek.speech.SpeechError;import com.iflytek.speech.SynthesizerPlayer;import com.iflytek.ui.RecognizerDialog;import com.iflytek.ui.RecognizerDialogListener;import com.iflytek.ui.SynthesizerDialog;import com.iflytek.ui.SynthesizerDialogListener;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Voice1Activity extends Activity {/** Called when the activity is first created. */// 声明控件private EditText et;private Button bt1;private Button bt2;private Button bt3;//全局只设一个String,因为String为final类型,这样做节省内存String text = "";private static final String APPID = "appid=4f2d3a06";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);bt1 = (Button) findViewById(R.id.bt_recognize);bt2 = (Button) findViewById(R.id.bt_speek);bt3 = (Button) findViewById(R.id.bt_speek_bg);et = (EditText) findViewById(R.id.et);// 初始化监听器initListener();}private void initListener() {bt1.setOnClickListener(myListener);bt2.setOnClickListener(myListener);bt3.setOnClickListener(myListener);}OnClickListener myListener = new OnClickListener() {@Overridepublic void onClick(View v) {// 根据不同View的id调用不同方法switch (v.getId()) {case R.id.bt_recognize:// 这是语言识别部分,最重要的实例化一个// RecognizerDialog并把你在官方网站申请的appid填入进去,非法id不能进行识别RecognizerDialog isrDialog = new RecognizerDialog(Voice1Activity.this, APPID);/* * 设置引擎目前支持五种 ”sms”:普通文本转写 “poi”:地名搜索 ”vsearch”:热词搜索 * ”video”:视频音乐搜索 ”asr”:命令词识别 */isrDialog.setEngine("sms", null, null);isrDialog.setListener(recoListener);isrDialog.show();break;case R.id.bt_speek:// 这是语言合成部分 同样需要实例化一个SynthesizerDialog ,并输入appidSynthesizerDialog syn = new SynthesizerDialog(Voice1Activity.this, APPID);syn.setListener(new SynthesizerDialogListener() {@Overridepublic void onEnd(SpeechError arg0) {}});// 根据EditText里的内容实现语音合成syn.setText(et.getText().toString(), null);syn.show();break;case R.id.bt_speek_bg:// 这是后台朗读,实例化一个SynthesizerPlayerSynthesizerPlayer player = SynthesizerPlayer.createSynthesizerPlayer(Voice1Activity.this, APPID);// 设置语音朗读者,可以根据需要设置男女朗读,具体请看api文档和官方论坛player.setVoiceName("vivixiaomei");player.playText(et.getText().toString(), "ent=vivi21,bft=5",null);break;default:break;}}};// 语言识别监听器,有两个方法RecognizerDialogListener recoListener = new RecognizerDialogListener() {@Overridepublic void onResults(ArrayList<RecognizerResult> results,boolean isLast) {// 服务器识别完成后会返回集合,我们这里就只得到最匹配的那一项text += results.get(0).text;System.out.println(text);}@Overridepublic void onEnd(SpeechError error) {if (error == null) {// 完成后就把结果显示在EditText上et.setText(text);}}};}
原创粉丝点击