讯飞语音集成

目前很多app都集成了语音搜索,语音输入等功能。科大讯飞的语音输入法比较厉害。这里就简单集成下科大讯飞的语音识别。

效果图

由于是集成第三方的,都是根据文档来的,不是很难。这里直接简单记录下,方便使用

当然,要想集成科大讯飞的语音功能,首先要去官方开放平台去注册账号,下载相关SDK,这个是必备的。

在开放平台创建新的应用

这样一步一步的进行,可以看到从官方下载的SDK里面doc文件夹里带有各种说明文档,按照文档来就行了。

代码

废话不多说,直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

package com.kevin.tech.voicetest;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedHashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// 语音听写对象
private SpeechRecognizer speechRecognizer;
// 语音听写UI
private RecognizerDialog recognizerDialog;
// 用HashMap存储听写结果
private HashMap<String, String> mRecoginizerResult = new LinkedHashMap<String, String>();
private static String TAG = MainActivity.class.getSimpleName();
// 引擎类型
private String mEngineType = SpeechConstant.TYPE_CLOUD;
private EditText mEtText;
private Button mBtnBegin, mBtnCancel;
// private SharedPreferences mSharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtText = (EditText) findViewById(R.id.tv_text);
mBtnBegin = (Button) findViewById(R.id.btn_begin);
mBtnCancel = (Button) findViewById(R.id.btn_cancel);
mBtnBegin.setOnClickListener(this);
mBtnCancel.setOnClickListener(this);
// 初始化识别无UI识别对象
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
speechRecognizer = SpeechRecognizer.createRecognizer(MainActivity.this, mInitListener);
// 初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
// 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
recognizerDialog = new RecognizerDialog(MainActivity.this, mInitListener);
}

/**
* 初始化监听器。
*/
private InitListener mInitListener = new InitListener() {

@Override
public void onInit(int code) {
Log.d(TAG, "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
showTip("初始化失败,错误码:" + code);
} else {
showTip("初始化成功");
}
}
};

private void showTip(String msg) {
ToastUtils.showToast(MainActivity.this, msg);
}

@Override
public void onClick(View v) {
if (null == speechRecognizer) {
// 创建单例失败,与 21001 错误为同样原因,参考 http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=9688
this.showTip("创建对象失败,请确认 libmsc.so 放置正确,且有调用 createUtility 进行初始化");
return;
}

switch (v.getId()) {
case R.id.btn_begin:
mEtText.setText(null);// 清空显示内容
mRecoginizerResult.clear();
// 设置参数
setParam();

recognizerDialog.setListener(mRecognizerDialogListener);
recognizerDialog.show();
showTip("请开始说话...");
break;
case R.id.btn_cancel:
speechRecognizer.cancel();
showTip("取消听写");
break;
}
}

/**
* 参数设置
*/
public void setParam() {
// 清空参数
speechRecognizer.setParameter(SpeechConstant.PARAMS, null);

// 设置听写引擎
speechRecognizer.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType);
// 设置返回结果格式
speechRecognizer.setParameter(SpeechConstant.RESULT_TYPE, "json");

// String lag = mSharedPreferences.getString("iat_language_preference", "mandarin");
String lag = "mandarin";
if (lag.equals("en_us")) {
// 设置语言
speechRecognizer.setParameter(SpeechConstant.LANGUAGE, "en_us");
} else {
// 设置语言
speechRecognizer.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// 设置语言区域
speechRecognizer.setParameter(SpeechConstant.ACCENT, lag);
}

// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
// speechRecognizer.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("iat_vadbos_preference", "4000"));
speechRecognizer.setParameter(SpeechConstant.VAD_BOS, "4000");

// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音
// speechRecognizer.setParameter(SpeechConstant.VAD_EOS, mSharedPreferences.getString("iat_vadeos_preference", "1000"));
speechRecognizer.setParameter(SpeechConstant.VAD_EOS, "1000");

// 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
// speechRecognizer.setParameter(SpeechConstant.ASR_PTT, mSharedPreferences.getString("iat_punc_preference", "1"));
speechRecognizer.setParameter(SpeechConstant.ASR_PTT, "1");

// 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
// 注:AUDIO_FORMAT参数语记需要更新版本才能生效
speechRecognizer.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
speechRecognizer.setParameter(SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/msc/iat.wav");
}

/**
* 听写UI监听器
*/
private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {
public void onResult(RecognizerResult results, boolean isLast) {
printResult(results);
}

/**
* 识别回调错误.
*/
public void onError(SpeechError error) {
showTip(error.getPlainDescription(true));
}

};

private void printResult(RecognizerResult results) {
String text = JsonParser.parseIatResult(results.getResultString());

String sn = null;
// 读取json结果中的sn字段
try {
JSONObject resultJson = new JSONObject(results.getResultString());
sn = resultJson.optString("sn");
} catch (JSONException e) {
e.printStackTrace();
}

mRecoginizerResult.put(sn, text);

StringBuffer resultBuffer = new StringBuffer();
for (String key : mRecoginizerResult.keySet()) {
resultBuffer.append(mRecoginizerResult.get(key));
}

mEtText.setText(resultBuffer.toString());
mEtText.setSelection(mEtText.length());
}

@Override
protected void onDestroy() {
super.onDestroy();

if (null != speechRecognizer) {
// 退出时释放连接
speechRecognizer.cancel();
speechRecognizer.destroy();
}
}
}

Demo下载