科大讯飞语音识别


转载请说明出处!
作者:kqw攻城狮
出处:个人站 | CSDN


离线语音识别(语记)

效果图

效果图

源码

下载地址:http://download.csdn.net/detail/q4878802/9032149

下载语记并安装离线资源

下载语记并安装离线资源

集成

初始化

在清单文件中application标签下添加

1
android:name="InitKqwSpeech"

初始化

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
package com.example.kqwlocalspeechdemo;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import android.app.Application;
public class InitKqwSpeech extends Application {
@Override
public void onCreate() {
// 应用程序入口处调用,避免手机内存过小,杀死后台进程后通过历史intent进入Activity造成SpeechUtility对象为null
// 如在Application中调用初始化,需要在Mainifest中注册该Applicaiton
// 注意:此接口在非主进程调用会返回null对象,如需在非主进程使用语音功能,请增加参数:SpeechConstant.FORCE_LOGIN+"=true"
// 参数间使用“,”分隔。
// 设置你申请的应用appid
StringBuffer param = new StringBuffer();
param.append("appid=55d33f09");
param.append(",");
param.append(SpeechConstant.ENGINE_MODE + "=" + SpeechConstant.MODE_MSC);
// param.append(",");
// param.append(SpeechConstant.FORCE_LOGIN + "=true");
SpeechUtility.createUtility(InitKqwSpeech.this, param.toString());
super.onCreate();
}
}

语音听写工具类

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
package com.example.kqwlocalspeechdemo.engine;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.example.kqwlocalspeechdemo.utils.JsonUtils;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
public abstract class KqwSpeechRecognizer {
/**
* 初始化的回调
*
* @param flag
* true 初始化成功 false 初始化失败
*/
public abstract void initListener(boolean flag);
public abstract void resultData(String data);
public abstract void speechLog(String log);
// 语音听写对象
private SpeechRecognizer mIat;
// TAG标签
private static String TAG = "KqwSpeechRecognizer";
// 上下文
private static Context mContext;
// SharedPreferences
// private SharedPreferences mSharedPreferences;
public KqwSpeechRecognizer(Context context) {
// 获取上下文
mContext = context;
// 初始化识别对象
mIat = SpeechRecognizer.createRecognizer(context, new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
initListener(false);
Toast.makeText(mContext, "初始化失败,错误码:" + code, Toast.LENGTH_SHORT).show();
} else {
initListener(true);
}
}
});
}
/**
* 开始录音
*/
public void startListening() {
// 设置参数
setParam();
// 不显示听写对话框
int ret = mIat.startListening(recognizerListener);
if (ret != ErrorCode.SUCCESS) {
Toast.makeText(mContext, "听写失败,错误码:" + ret, Toast.LENGTH_SHORT).show();
}
}
/**
* 停止录音并获取录入的文字
*/
public void cancel() {
if (null != mIat) {
// mIat.cancel();
mIat.stopListening();
}
}
/**
* 听写监听器。
*/
private RecognizerListener recognizerListener = new RecognizerListener() {
StringBuffer resultText;
@Override
public void onBeginOfSpeech() {
speechLog("开始说话");
Log.i(TAG, "开始说话");
resultText = new StringBuffer();
}
@Override
public void onError(SpeechError error) {
Log.i(TAG, error.getPlainDescription(true));
}
@Override
public void onEndOfSpeech() {
speechLog("结束说话");
Log.i(TAG, "结束说话");
}
@Override
public void onResult(RecognizerResult results, boolean isLast) {
Log.d(TAG, results.getResultString());
String text = JsonUtils.parseIatResult(results.getResultString());
resultText.append(text);
if (isLast) {
// 最后的结果
resultData(resultText.toString().trim());
}
}
@Override
public void onVolumeChanged(int volume, byte[] data) {
speechLog("当前正在说话,音量大小:" + volume);
Log.i(TAG, "当前正在说话,音量大小:" + volume);
}
@Override
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
}
};
/**
* 参数设置
*
* @param param
* @return
*/
public void setParam() {
// 清空参数
mIat.setParameter(SpeechConstant.PARAMS, null);
// 设置听写引擎
mIat.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
// 设置返回结果格式
mIat.setParameter(SpeechConstant.RESULT_TYPE, "json");
// 设置语言
mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// 设置语言区域
mIat.setParameter(SpeechConstant.ACCENT, "zh_cn");
// 设置语音前端点
mIat.setParameter(SpeechConstant.VAD_BOS, "4000");
// 设置语音后端点
mIat.setParameter(SpeechConstant.VAD_EOS, "1000");
// 设置标点符号
mIat.setParameter(SpeechConstant.ASR_PTT, "1");
// 设置音频保存路径
// mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH,
// Environment.getExternalStorageDirectory() + "/iflytek/wavaudio.pcm");
}
}

工具类

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
package com.example.kqwlocalspeechdemo.utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
public class JsonUtils {
public static String parseIatResult(String json) {
StringBuffer ret = new StringBuffer();
try {
JSONTokener tokener = new JSONTokener(json);
JSONObject joResult = new JSONObject(tokener);
JSONArray words = joResult.getJSONArray("ws");
for (int i = 0; i < words.length(); i++) {
// 转写结果词,默认使用第一个结果
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
JSONObject obj = items.getJSONObject(0);
ret.append(obj.getString("w"));
// 如果需要多候选结果,解析数组其他字段
// for(int j = 0; j < items.length(); j++)
// {
// JSONObject obj = items.getJSONObject(j);
// ret.append(obj.getString("w"));
// }
}
} catch (Exception e) {
e.printStackTrace();
}
return ret.toString();
}
}

测试类

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
package com.example.kqwlocalspeechdemo;
import com.example.kqwlocalspeechdemo.engine.KqwSpeechRecognizer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView mTvResult;
private TextView mTvLog;
private KqwSpeechRecognizer mKqwSpeechRecognizer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvResult = (TextView) findViewById(R.id.tv_result);
mTvLog = (TextView) findViewById(R.id.tv_log);
// 初始化语音听写识别器
mKqwSpeechRecognizer = new KqwSpeechRecognizer(this) {
@Override
public void speechLog(String log) {
mTvLog.setText(log);
}
@Override
public void resultData(String data) {
mTvResult.setText(data);
}
@Override
public void initListener(boolean flag) {
if(flag){
Toast.makeText(MainActivity.this, "初始化成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "初始化失败", Toast.LENGTH_SHORT).show();
}
}
};
}
/**
* 开始识别按钮
*
* @param view
*/
public void start(View view) {
mTvResult.setText(null);
// 开始识别
mKqwSpeechRecognizer.startListening();
}
}

页面布局

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
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kqwlocalspeechdemo.MainActivity" >
<Button
android:id="@+id/bt_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="start"
android:text="开始录音" />
<TextView
android:id="@+id/tv_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/bt_start"
android:gravity="center"
android:text="录音信息" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tv_log" >
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回结果" />
</ScrollView>
</RelativeLayout>

在线语音识别

在线语音听写和离线语音听写基本一样,只要修改一下识别引擎即可。(就可以不用语记了)

1
2
// 设置听写引擎
mIat.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
坚持原创技术分享,您的支持将鼓励我继续创作!