package com.example.voicetotext;
import java.util.ArrayList;
import com.baidu.voicerecognition.android.VoiceRecognitionConfig;
import com.baidu.voicerecognition.android.ui.BaiduASRDigitalDialog;
import com.baidu.voicerecognition.android.ui.DialogRecognitionListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
private static final String tag = "VoiceToText";
// 开始按钮
private Button BtnStart;
private ImageButton imageButton;
// 文本框
private EditText InputBox;
// 百度语音识别对话框
private BaiduASRDigitalDialog mDialog = null;
private DialogRecognitionListener mDialogListener = null;
// 应用授权信息 ,这里使用了官方SDK中的参数,如果需要,请自行申请,并修改为自己的授权信息
// private String API_KEY = "SKcubzZwK3oTWHZGns6mqc52";
// private String SECRET_KEY = "VYMxa26yXK5mbtEauET3pFUvnbL4XAWX";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.BtnStart = (Button) findViewById(R.id.button1);
this.InputBox = (EditText) findViewById(R.id.editText1);
this.imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(this);
// BtnStart.setOnClickListener(this);
if (mDialog == null) {
if (mDialog != null) {
mDialog.dismiss();
}
}
// 设置百度语音识别回调接口
mDialogListener = new DialogRecognitionListener() {
@Override
public void onResults(Bundle mResults) {
ArrayList<String> rs = mResults != null ? mResults
.getStringArrayList(RESULTS_RECOGNITION) : null;
if (rs != null && rs.size() > 0) {
// InputBox.setText(rs.get(0));
InputBox.append(rs.toString());
}
}
};
baiduset();
}
private void baiduset() {
// TODO Auto-generated method stub
Bundle params = new Bundle();
// 设置API_KEY, SECRET_KEY
params.putString(BaiduASRDigitalDialog.PARAM_API_KEY, Cantent.API_KEY);
params.putString(BaiduASRDigitalDialog.PARAM_SECRET_KEY,
Cantent.SECRET_KEY);
// 设置识别领域:搜索、输入、地图、音乐……,可选。默认为输入。
// 设置语音识别模式为输入模式N
params.putInt(BaiduASRDigitalDialog.PARAM_PROP,
VoiceRecognitionConfig.PROP_INPUT);
// 设置语音识别对话框为蓝色高亮主题
params.putInt(BaiduASRDigitalDialog.PARAM_DIALOG_THEME,
BaiduASRDigitalDialog.THEME_ORANGE_LIGHTBG);
// 实例化百度语音识别对话框
mDialog = new BaiduASRDigitalDialog(this, params);
mDialog.setDialogRecognitionListener(mDialogListener);
// 如果需要语义解析,设置下方参数。领域为输入不支持
mDialog.getParams().putBoolean(BaiduASRDigitalDialog.PARAM_NLU_ENABLE,
false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_BACK) {
MainActivity.this.finish();
Log.e(tag, "finish");
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
InputBox.setText(null);
mDialog.show();
break;
case R.id.imageButton1:
InputBox.setText(null);
mDialog.show();
break;
default:
break;
}
}
}
import java.util.ArrayList;
import com.baidu.voicerecognition.android.VoiceRecognitionConfig;
import com.baidu.voicerecognition.android.ui.BaiduASRDigitalDialog;
import com.baidu.voicerecognition.android.ui.DialogRecognitionListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
private static final String tag = "VoiceToText";
// 开始按钮
private Button BtnStart;
private ImageButton imageButton;
// 文本框
private EditText InputBox;
// 百度语音识别对话框
private BaiduASRDigitalDialog mDialog = null;
private DialogRecognitionListener mDialogListener = null;
// 应用授权信息 ,这里使用了官方SDK中的参数,如果需要,请自行申请,并修改为自己的授权信息
// private String API_KEY = "SKcubzZwK3oTWHZGns6mqc52";
// private String SECRET_KEY = "VYMxa26yXK5mbtEauET3pFUvnbL4XAWX";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.BtnStart = (Button) findViewById(R.id.button1);
this.InputBox = (EditText) findViewById(R.id.editText1);
this.imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(this);
// BtnStart.setOnClickListener(this);
if (mDialog == null) {
if (mDialog != null) {
mDialog.dismiss();
}
}
// 设置百度语音识别回调接口
mDialogListener = new DialogRecognitionListener() {
@Override
public void onResults(Bundle mResults) {
ArrayList<String> rs = mResults != null ? mResults
.getStringArrayList(RESULTS_RECOGNITION) : null;
if (rs != null && rs.size() > 0) {
// InputBox.setText(rs.get(0));
InputBox.append(rs.toString());
}
}
};
baiduset();
}
private void baiduset() {
// TODO Auto-generated method stub
Bundle params = new Bundle();
// 设置API_KEY, SECRET_KEY
params.putString(BaiduASRDigitalDialog.PARAM_API_KEY, Cantent.API_KEY);
params.putString(BaiduASRDigitalDialog.PARAM_SECRET_KEY,
Cantent.SECRET_KEY);
// 设置识别领域:搜索、输入、地图、音乐……,可选。默认为输入。
// 设置语音识别模式为输入模式N
params.putInt(BaiduASRDigitalDialog.PARAM_PROP,
VoiceRecognitionConfig.PROP_INPUT);
// 设置语音识别对话框为蓝色高亮主题
params.putInt(BaiduASRDigitalDialog.PARAM_DIALOG_THEME,
BaiduASRDigitalDialog.THEME_ORANGE_LIGHTBG);
// 实例化百度语音识别对话框
mDialog = new BaiduASRDigitalDialog(this, params);
mDialog.setDialogRecognitionListener(mDialogListener);
// 如果需要语义解析,设置下方参数。领域为输入不支持
mDialog.getParams().putBoolean(BaiduASRDigitalDialog.PARAM_NLU_ENABLE,
false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_BACK) {
MainActivity.this.finish();
Log.e(tag, "finish");
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
InputBox.setText(null);
mDialog.show();
break;
case R.id.imageButton1:
InputBox.setText(null);
mDialog.show();
break;
default:
break;
}
}
}