package com.iflytek.voicedemo;
import java.util.HashMap;
import java.util.LinkedHashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;
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;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.iflytek.speech.util.IatSettings;
import com.iflytek.speech.util.JsonParser;
import com.iflytek.sunflower.FlowerCollector;
public class IatDemo extends Activity implements OnClickListener {
private static String TAG = IatDemo.class.getSimpleName();
// 语音听写对象
private SpeechRecognizer mIat;
// 语音听写UI
private RecognizerDialog mIatDialog;
// 用HashMap存储听写结果
private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();
private EditText mResultText;
private Toast mToast;
private SharedPreferences mSharedPreferences;
// 引擎类型
private String mEngineType = SpeechConstant.TYPE_CLOUD;
@SuppressLint("ShowToast")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.iatdemo);
initLayout();
// 初始化识别无UI识别对象
// 使用SpeechRecognizer对象,可根据回调消息自定义界面;
mIat = SpeechRecognizer.createRecognizer(IatDemo.this, mInitListener);
// 初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer
// 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
mIatDialog = new RecognizerDialog(IatDemo.this, mInitListener);
mSharedPreferences = getSharedPreferences(IatSettings.PREFER_NAME, Activity.MODE_PRIVATE);
mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
mResultText = ((EditText) findViewById(R.id.iat_text));
// mInstaller = new ApkInstaller(IatDemo.this);
}
//初始化Layout
private void initLayout() {
findViewById(R.id.iat_recognize).setOnClickListener(IatDemo.this);
findViewById(R.id.image_iat_set).setOnClickListener(IatDemo.this);
}
int ret = 0; // 函数调用返回值
@Override
public void onClick(View view) {
if (null == mIat) {
// 创建单例失败,与 21001 错误为同样原因,参考
// http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=9688
this.showTip("创建对象失败,请确认 libmsc.so 放置正确,且有调用 createUtility 进行初始化");
return;
}
switch (view.getId()) {
// 进入参数设置页面
case R.id.image_iat_set:
Intent intents = new Intent(IatDemo.this, IatSettings.class);
startActivity(intents);
break;
// 开始听写
// 如何判断一次听写结束:OnResult isLast=true 或者 onError
case R.id.iat_recognize:
// 移动数据分析,收集开始听写事件
FlowerCollector.onEvent(IatDemo.this, "iat_recognize");
mResultText.setText(null);// 清空显示内容
mIatResults.clear();
// 设置参数
setParam();
// 显示听写对话框
mIatDialog.setListener(mRecognizerDialogListener);
mIatDialog.show();
showTip(getString(R.string.text_begin));
break;
default:
break;
}
}
// 初始化监听器
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
showTip("初始化失败,错误码:" + code);
}
}
};
//听写监听器
private RecognizerListener mRecognizerListener = new RecognizerListener() {
@Override
public void onBeginOfSpeech() {
// 此回调表示:sdk内部录音机已经准备好了,用户可以开始语音输入
showTip("开始说话");
}
@Override
public void onError(SpeechError error) {
// Tips:
// 错误码:10118(您没有说话),可能是录音机权限被禁,需要提示用户打开应用的录音权限。
// 如果使用本地功能(语记)需要提示用户开启语记的录音权限。
showTip(error.getPlainDescription(true));
}
@Override
public void onEndOfSpeech() {
// 此回调表示:检测到了语音的尾端点,已经进入识别过程,不再接受语音输入
showTip("结束说话");
}
@Override
public void onResult(RecognizerResult results, boolean isLast) {
Log.d(TAG, results.getResultString());
printResult(results);
if (isLast) {
// TODO 最后的结果
}
}
@Override
public void onVolumeChanged(int volume, byte[] data) {
showTip("当前正在说话,音量大小:" + volume);
Log.d(TAG, "返回音频数据:" + data.length);
}
@Override
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
// 以下代码用于获取与云端的会话id,当业务出错时将会话id提供给技术支持人员,可用于查询会话日志,定位出错原因
// 若使用本地能力,会话id为null
// if (SpeechEvent.EVENT_SESSION_ID == eventType) {
// String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
// Log.d(TAG, "session id =" + sid);
// }
}
};
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();
}
mIatResults.put(sn, text);
StringBuffer resultBuffer = new StringBuffer();
for (String key : mIatResults.keySet()) {
resultBuffer.append(mIatResults.get(key));
}
mResultText.setText(resultBuffer.toString());
mResultText.setSelection(mResultText.length());
}
//听写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 showTip(final String str) {
mToast.setText(str);
mToast.show();
}
//参数设置
public void setParam() {
// 清空参数
mIat.setParameter(SpeechConstant.PARAMS, null);
// 设置听写引擎
mIat.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType);
// 设置返回结果格式
mIat.setParameter(SpeechConstant.RESULT_TYPE, "json");
String lag = mSharedPreferences.getString("iat_language_preference", "mandarin");
if (lag.equals("en_us")) {
// 设置语言
mIat.setParameter(SpeechConstant.LANGUAGE, "en_us");
} else {
// 设置语言
mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
// 设置语言区域
mIat.setParameter(SpeechConstant.ACCENT, lag);
}
// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
mIat.setParameter(SpeechConstant.VAD_BOS, mSharedPreferences.getString("iat_vadbos_preference", "4000"));
// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SpeechDemo.7z (88个子文件)
SpeechDemo
.project 846B
default.properties 393B
bin
AndroidManifest.xml 3KB
jarlist.cache 120B
dexedLibs
Msc-00db672ed1597b1670a268d274ebf41b.jar 152KB
Sunflower-4e863307d1e455156501e48880e7d46e.jar 25KB
Sunflower-0adddc03bbca64cdff9c79f9acde9a10.jar 25KB
android-support-v4-abe6727cbaa826f7ee86b33bbb9d5000.jar 148KB
Msc-ae6f8574e0e7f44e191442ce74c00977.jar 152KB
android-support-v4-2b280c20f7b2829f66347ae15d701d78.jar 148KB
res
crunch
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable
setting.png 4KB
setting_p.png 2KB
drawable-mdpi
icon.png 2KB
resources.ap_ 198KB
classes
com
iflytek
voicedemo
R$string.class 1KB
R$layout.class 434B
IatDemo$1.class 1KB
MainActivity.class 1KB
R$dimen.class 533B
IatDemo$3.class 1KB
R$drawable.class 632B
R$array.class 1KB
R.class 803B
R$xml.class 514B
R$style.class 402B
R$color.class 517B
BuildConfig.class 351B
SpeechApp.class 800B
R$attr.class 349B
IatDemo.class 7KB
MainActivity$1.class 1KB
IatDemo$2.class 2KB
R$id.class 530B
speech
util
JsonParser.class 3KB
IatSettings.class 2KB
SettingTextWatcher.class 3KB
FucUtil.class 4KB
classes.dex 1.37MB
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 57B
org.eclipse.jdt.core.prefs 177B
lint.xml 53B
AndroidManifest.xml 3KB
libs
Sunflower.jar 55KB
Msc.jar 359KB
armeabi
libmsc.so 1.12MB
android-support-v4.jar 384KB
src
com
iflytek
voicedemo
SpeechApp.java 1KB
IatDemo.java 9KB
MainActivity.java 809B
speech
util
SettingTextWatcher.java 2KB
IatSettings.java 1KB
JsonParser.java 3KB
FucUtil.java 4KB
project.properties 563B
res
values
styles.xml 606B
dimen.xml 302B
colors.xml 484B
strings.xml 7KB
layout
iatdemo.xml 3KB
main.xml 415B
xml
tts_setting.xml 1KB
iat_setting.xml 1KB
understand_setting.xml 2KB
ise_settings.xml 1KB
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 5KB
drawable
setting.png 4KB
setting_p.png 2KB
list_bg_color.xml 285B
main_setting_btn_np.xml 512B
anim
drawable-mdpi
icon.png 3KB
drawable-xhdpi
.classpath 491B
gen
com
iflytek
voicedemo
R.java 5KB
BuildConfig.java 163B
assets
call.bnf 324B
iflytek
voice_bg.9.png 1KB
warning.png 8KB
voice_full.png 16KB
waiting.png 12KB
voice_empty.png 14KB
recognize.xml 3KB
userwords 211B
grammar_sample.abnf 165B
iattest.wav 109KB
keys 10KB
共 88 条
- 1
资源评论
- ly6292017-10-16评论内容不能少于
- chrispaul3312018-11-13简单的实现了语音转文字的接口。
__毛豆
- 粉丝: 216
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功