科大訊飛語音識(shí)別集成
作者:DDstar
集成科大訊飛的語音識(shí)別功能 科大訊飛的優(yōu)勢(shì)是識(shí)別精度高
源碼簡(jiǎn)介:集成科大訊飛的語音識(shí)別功能 科大訊飛的優(yōu)勢(shì)是識(shí)別精度高
源碼效果:
源碼片段:
- package com.example.testmodel;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- 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;
- public class MainActivity extends Activity {
- private TextView mResult;
- private SpeechRecognizer mIat;
- // 語音聽寫UI
- private RecognizerDialog mIatDialog;
- // 用HashMap存儲(chǔ)聽寫結(jié)果
- private HashMap<string, string=""> mIatResults = new LinkedHashMap<string, string="">();
- private EditText mResultText;
- private Toast mToast;
- private SharedPreferences mSharedPreferences;
- // 引擎類型
- private String mEngineType = SpeechConstant.TYPE_CLOUD;
- // 語記安裝助手類
- ApkInstaller mInstaller;
- private int ret = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mEngineType = SpeechConstant.TYPE_CLOUD;
- mResult = (TextView) findViewById(R.id.textView1);
- mIat = SpeechRecognizer.createRecognizer(MainActivity.this, mInitListener);
- // 初始化聽寫Dialog,如果只使用有UI聽寫功能,無需創(chuàng)建SpeechRecognizer
- // 使用UI聽寫功能,請(qǐng)根據(jù)sdk文件目錄下的notice.txt,放置布局文件和圖片資源
- mIatDialog = new RecognizerDialog(MainActivity.this, mInitListener);
- mSharedPreferences = getSharedPreferences("YOU",
- Activity.MODE_PRIVATE);
- mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
- mInstaller = new ApkInstaller(MainActivity.this);
- }
- public void startRec(View v) {
- mResult.setText(null);// 清空顯示內(nèi)容
- mIatResults.clear();
- // 設(shè)置參數(shù)
- setParam();
- boolean isShowDialog = mSharedPreferences.getBoolean(
- "iat_show", true);
- if (isShowDialog) {
- // 顯示聽寫對(duì)話框
- mIatDialog.setListener(mRecognizerDialogListener);
- mIatDialog.show();
- showTip("請(qǐng)開始說話…");
- } else {
- // 不顯示聽寫對(duì)話框
- ret = mIat.startListening(mRecognizerListener);
- if (ret != ErrorCode.SUCCESS) {
- showTip("聽寫失敗,錯(cuò)誤碼:" + ret);
- } else {
- showTip("請(qǐng)開始說話…");
- }
- }
- }
- /**
- * 聽寫監(jiān)聽器。
- */
- private RecognizerListener mRecognizerListener = new RecognizerListener() {
- @Override
- public void onBeginOfSpeech() {
- // 此回調(diào)表示:sdk內(nèi)部錄音機(jī)已經(jīng)準(zhǔn)備好了,用戶可以開始語音輸入
- showTip("開始說話");
- }
- @Override
- public void onError(SpeechError error) {
- // Tips:
- // 錯(cuò)誤碼:10118(您沒有說話),可能是錄音機(jī)權(quán)限被禁,需要提示用戶打開應(yīng)用的錄音權(quán)限。
- // 如果使用本地功能(語記)需要提示用戶開啟語記的錄音權(quán)限。
- showTip(error.getPlainDescription(true));
- }
- @Override
- public void onEndOfSpeech() {
- // 此回調(diào)表示:檢測(cè)到了語音的尾端點(diǎn),已經(jīng)進(jìn)入識(shí)別過程,不再接受語音輸入
- showTip("結(jié)束說話");
- }
- @Override
- public void onResult(RecognizerResult results, boolean isLast) {
- // Log.d(TAG, results.getResultString());
- printResult(results);
- if (isLast) {
- // TODO ***的結(jié)果
- }
- }
- @Override
- public void onVolumeChanged(int volume, byte[] data) {
- showTip("當(dāng)前正在說話,音量大小:" + volume);
- // Log.d(TAG, "返回音頻數(shù)據(jù):"+data.length);
- }
- @Override
- public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
- // 以下代碼用于獲取與云端的會(huì)話id,當(dāng)業(yè)務(wù)出錯(cuò)時(shí)將會(huì)話id提供給技術(shù)支持人員,可用于查詢會(huì)話日志,定位出錯(cuò)原因
- // 若使用本地能力,會(huì)話id為null
- // if (SpeechEvent.EVENT_SESSION_ID == eventType) {
- // String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
- // Log.d(TAG, "session id =" + sid);
- // }
- }
- };
- /**
- * 聽寫UI監(jiān)聽器
- */
- private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {
- public void onResult(RecognizerResult results, boolean isLast) {
- printResult(results);
- }
- /**
- * 識(shí)別回調(diào)錯(cuò)誤.
- */
- public void onError(SpeechError error) {
- showTip(error.getPlainDescription(true));
- }
- };
- private void showTip(final String str) {
- mToast.setText(str);
- mToast.show();
- }
- private void printResult(RecognizerResult results) {
- String text = JsonParser.parseIatResult(results.getResultString());
- String sn = null;
- // 讀取json結(jié)果中的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));
- }
- mResult.setText(resultBuffer.toString());
- // mResult.setSelection(mResultText.length());
- }
- /**
- * 初始化監(jiān)聽器。
- */
- private InitListener mInitListener = new InitListener() {
- @Override
- public void onInit(int code) {
- // Log.d(TAG, "SpeechRecognizer init() code = " + code);
- if (code != ErrorCode.SUCCESS) {
- // showTip("初始化失敗,錯(cuò)誤碼:" + code);
- }
- }
- };
- /**
- * 參數(shù)設(shè)置
- *
- * @param param
- * @return
- */
- public void setParam() {
- // 清空參數(shù)
- mIat.setParameter(SpeechConstant.PARAMS, null);
- // 設(shè)置聽寫引擎
- mIat.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType);
- // 設(shè)置返回結(jié)果格式
- mIat.setParameter(SpeechConstant.RESULT_TYPE, "json");
- String lag = mSharedPreferences.getString("iat_language_preference",
- "mandarin");
- if (lag.equals("en_us")) {
- // 設(shè)置語言
- mIat.setParameter(SpeechConstant.LANGUAGE, "en_us");
- } else {
- // 設(shè)置語言
- mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
- // 設(shè)置語言區(qū)域
- mIat.setParameter(SpeechConstant.ACCENT, lag);
- }
- // 設(shè)置語音前端點(diǎn):靜音超時(shí)時(shí)間,即用戶多長(zhǎng)時(shí)間不說話則當(dāng)做超時(shí)處理
- mIat.setParameter(SpeechConstant.VAD_BOS,
- mSharedPreferences.getString("iat_vadbos_preference", "4000"));
- // 設(shè)置語音后端點(diǎn):后端點(diǎn)靜音檢測(cè)時(shí)間,即用戶停止說話多長(zhǎng)時(shí)間內(nèi)即認(rèn)為不再輸入, 自動(dòng)停止錄音
- mIat.setParameter(SpeechConstant.VAD_EOS,
- mSharedPreferences.getString("iat_vadeos_preference", "1000"));
- // 設(shè)置標(biāo)點(diǎn)符號(hào),設(shè)置為"0"返回結(jié)果無標(biāo)點(diǎn),設(shè)置為"1"返回結(jié)果有標(biāo)點(diǎn)
- mIat.setParameter(SpeechConstant.ASR_PTT,
- mSharedPreferences.getString("iat_punc_preference", "1"));
- // 設(shè)置音頻保存路徑,保存音頻格式支持pcm、wav,設(shè)置路徑為sd卡請(qǐng)注意WRITE_EXTERNAL_STORAGE權(quán)限
- // 注:AUDIO_FORMAT參數(shù)語記需要更新版本才能生效
- mIat.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
- mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH,
- Environment.getExternalStorageDirectory() + "/msc/iat.wav");
- // 設(shè)置聽寫結(jié)果是否結(jié)果動(dòng)態(tài)修正,為“1”則在聽寫過程中動(dòng)態(tài)遞增地返回結(jié)果,否則只在聽寫結(jié)束之后返回最終結(jié)果
- // 注:該參數(shù)暫時(shí)只對(duì)在線聽寫有效
- mIat.setParameter(SpeechConstant.ASR_DWA,
- mSharedPreferences.getString("iat_dwa_preference", "0"));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- </string,></string,>
責(zé)任編輯:倪明
來源:
devstore