@Override protected void onDestroy() { super.onDestroy(); // 退出时释放连接 mSpeechUnderstander.cancel(); mSpeechUnderstander.destroy(); if (mTextUnderstander.isUnderstanding()) mTextUnderstander.cancel(); mTextUnderstander.destroy(); }
@Override public void onClick(View view) { switch (view.getId()) { // 进入参数设置页面 case R.id.image_understander_set: Intent intent = new Intent(UnderstanderDemo.this, UnderstanderSettings.class); startActivity(intent); break; // 开始文本理解 case R.id.text_understander: mUnderstanderText.setText(""); String text = "合肥明天的天气怎么样?"; showTip(text); if (mTextUnderstander.isUnderstanding()) { mTextUnderstander.cancel(); showTip("取消"); } else { ret = mTextUnderstander.understandText(text, textListener); if (ret != 0) { showTip("语义理解失败,错误码:" + ret); } } break; // 开始语音理解 case R.id.start_understander: mUnderstanderText.setText(""); // 设置参数 setParam(); if (mSpeechUnderstander.isUnderstanding()) { // 开始前检查状态 mSpeechUnderstander.stopUnderstanding(); showTip("停止录音"); } else { ret = mSpeechUnderstander.startUnderstanding(mRecognizerListener); if (ret != 0) { showTip("语义理解失败,错误码:" + ret); } else { showTip(getString(R.string.text_begin)); } } break; // 停止语音理解 case R.id.understander_stop: mSpeechUnderstander.stopUnderstanding(); showTip("停止语义理解"); break; // 取消语音理解 case R.id.understander_cancel: mSpeechUnderstander.cancel(); showTip("取消语义理解"); break; default: break; } }
/** * 参数设置 * * @param param * @return */ public void setParam() { String lag = mSharedPreferences.getString("understander_language_preference", "mandarin"); if (lag.equals("en_us")) { // 设置语言 mSpeechUnderstander.setParameter(SpeechConstant.LANGUAGE, "en_us"); } else { // 设置语言 mSpeechUnderstander.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); // 设置语言区域 mSpeechUnderstander.setParameter(SpeechConstant.ACCENT, lag); } // 设置语音前端点 mSpeechUnderstander.setParameter( SpeechConstant.VAD_BOS, mSharedPreferences.getString("understander_vadbos_preference", "4000")); // 设置语音后端点 mSpeechUnderstander.setParameter( SpeechConstant.VAD_EOS, mSharedPreferences.getString("understander_vadeos_preference", "1000")); // 设置标点符号 mSpeechUnderstander.setParameter( SpeechConstant.ASR_PTT, mSharedPreferences.getString("understander_punc_preference", "1")); // 设置音频保存路径 mSpeechUnderstander.setParameter( SpeechConstant.ASR_AUDIO_PATH, Environment.getExternalStorageDirectory() + "/iflytek/wavaudio.pcm"); }
public static void init(Context context) { m_instance = new XFSpeechUnderstander(); m_ctx = context; m_understander = SpeechUnderstander.createUnderstander(context, initListener); m_understander.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); // 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理 m_understander.setParameter(SpeechConstant.VAD_BOS, "10000"); // 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音 m_understander.setParameter(SpeechConstant.VAD_EOS, "2000"); // 设置标点符号,默认:1(有标点) m_understander.setParameter(SpeechConstant.ASR_PTT, "0"); // 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限 // 注:AUDIO_FORMAT参数语记需要更新版本才能生效 // m_understander.setParameter(SpeechConstant.AUDIO_FORMAT, "wav"); // m_understander.setParameter(SpeechConstant.ASR_AUDIO_PATH, // Environment.getExternalStorageDirectory() + "/msc/sud.wav"); }
@SuppressLint("ShowToast") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.understander); initLayout(); // 初始化对象 mSpeechUnderstander = SpeechUnderstander.createUnderstander(UnderstanderDemo.this, speechUnderstanderListener); mTextUnderstander = TextUnderstander.createTextUnderstander(UnderstanderDemo.this, textUnderstanderListener); mToast = Toast.makeText(UnderstanderDemo.this, "", Toast.LENGTH_SHORT); }
public boolean is_starting() { return m_understander.isUnderstanding(); }
public void destroy() { m_understander.destroy(); }
public void cancel() { m_understander.cancel(); }
public void stop() { if (m_understander.isUnderstanding()) m_understander.stopUnderstanding(); }
public void start(XFUnderstanderListener listener) { if (!m_understander.isUnderstanding()) m_understander.startUnderstanding(listener); }