科大离线语音听写需要安装语记(曾用名-语音+),暂时不支持离线包,可能是因为库会比较大。
下载地址:http://download.csdn.net/detail/q4878802/9032149
地址: http://www.xfyun.cn/speechservice
点击如果您是用户切换到用户界面然后再点击马上下载
如果再开发者界面点击下载,下载的是SDK
安装以后,在语记里点击能力->识别能力->离线识别资源,把基础资源和离线识别都下载下来
到此离线资源准备完成
在清单文件中application标签下添加
android:name="InitKqwSpeech"
初始化
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();
}
}
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");
}
}
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();
}
}
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();
}
}
<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>