zl程序教程

您现在的位置是:首页 >  APP

当前栏目

科大讯飞语音识别集成

2023-04-18 14:54:04 时间

源码简介:集成科大讯飞的语音识别功能 科大讯飞的优势是识别精度高

源码效果:

源码片段:

  1. package com.example.testmodel; 
  2.   
  3. import java.util.HashMap; 
  4. import java.util.LinkedHashMap; 
  5.   
  6. import org.json.JSONException; 
  7. import org.json.JSONObject; 
  8.   
  9. import android.app.Activity; 
  10. import android.content.SharedPreferences; 
  11. import android.os.Bundle; 
  12. import android.os.Environment; 
  13. import android.view.Menu; 
  14. import android.view.MenuItem; 
  15. import android.view.View; 
  16. import android.widget.EditText; 
  17. import android.widget.TextView; 
  18. import android.widget.Toast; 
  19.   
  20. import com.iflytek.cloud.ErrorCode; 
  21. import com.iflytek.cloud.InitListener; 
  22. import com.iflytek.cloud.RecognizerListener; 
  23. import com.iflytek.cloud.RecognizerResult; 
  24. import com.iflytek.cloud.SpeechConstant; 
  25. import com.iflytek.cloud.SpeechError; 
  26. import com.iflytek.cloud.SpeechRecognizer; 
  27. import com.iflytek.cloud.ui.RecognizerDialog; 
  28. import com.iflytek.cloud.ui.RecognizerDialogListener; 
  29.   
  30. public class MainActivity extends Activity { 
  31.   
  32.     private TextView mResult; 
  33.     private SpeechRecognizer mIat; 
  34.     // 语音听写UI 
  35.     private RecognizerDialog mIatDialog; 
  36.     // 用HashMap存储听写结果 
  37.     private HashMap<string, string=""> mIatResults = new LinkedHashMap<string, string="">(); 
  38.   
  39.     private EditText mResultText; 
  40.     private Toast mToast; 
  41.     private SharedPreferences mSharedPreferences; 
  42.     // 引擎类型 
  43.     private String mEngineType = SpeechConstant.TYPE_CLOUD; 
  44.     // 语记安装助手类 
  45.     ApkInstaller mInstaller; 
  46.     private int ret = 0
  47.   
  48.     @Override 
  49.     protected void onCreate(Bundle savedInstanceState) { 
  50.         super.onCreate(savedInstanceState); 
  51.         setContentView(R.layout.activity_main); 
  52.         mEngineType = SpeechConstant.TYPE_CLOUD; 
  53.         mResult = (TextView) findViewById(R.id.textView1); 
  54.         mIat = SpeechRecognizer.createRecognizer(MainActivity.this, mInitListener); 
  55.   
  56.         // 初始化听写Dialog,如果只使用有UI听写功能,无需创建SpeechRecognizer 
  57.         // 使用UI听写功能,请根据sdk文件目录下的notice.txt,放置布局文件和图片资源 
  58.         mIatDialog = new RecognizerDialog(MainActivity.this, mInitListener); 
  59.   
  60.         mSharedPreferences = getSharedPreferences("YOU"
  61.                 Activity.MODE_PRIVATE); 
  62.         mToast = Toast.makeText(this"", Toast.LENGTH_SHORT); 
  63.         mInstaller = new ApkInstaller(MainActivity.this); 
  64.   
  65.     } 
  66.   
  67.     public void startRec(View v) { 
  68.         mResult.setText(null);// 清空显示内容 
  69.         mIatResults.clear(); 
  70.         // 设置参数 
  71.         setParam(); 
  72.         boolean isShowDialog = mSharedPreferences.getBoolean( 
  73.                 "iat_show"true); 
  74.         if (isShowDialog) { 
  75.             // 显示听写对话框 
  76.             mIatDialog.setListener(mRecognizerDialogListener); 
  77.             mIatDialog.show(); 
  78.             showTip("请开始说话…"); 
  79.         } else { 
  80.             // 不显示听写对话框 
  81.             ret = mIat.startListening(mRecognizerListener); 
  82.             if (ret != ErrorCode.SUCCESS) { 
  83.                 showTip("听写失败,错误码:" + ret); 
  84.             } else { 
  85.                 showTip("请开始说话…"); 
  86.             } 
  87.         } 
  88.     } 
  89.       
  90.     /** 
  91.      * 听写监听器。 
  92.      */ 
  93.     private RecognizerListener mRecognizerListener = new RecognizerListener() { 
  94.   
  95.         @Override 
  96.         public void onBeginOfSpeech() { 
  97.             // 此回调表示:sdk内部录音机已经准备好了,用户可以开始语音输入 
  98.             showTip("开始说话"); 
  99.         } 
  100.   
  101.         @Override 
  102.         public void onError(SpeechError error) { 
  103.             // Tips: 
  104.             // 错误码:10118(您没有说话),可能是录音机权限被禁,需要提示用户打开应用的录音权限。 
  105.             // 如果使用本地功能(语记)需要提示用户开启语记的录音权限。 
  106.             showTip(error.getPlainDescription(true)); 
  107.         } 
  108.   
  109.         @Override 
  110.         public void onEndOfSpeech() { 
  111.             // 此回调表示:检测到了语音的尾端点,已经进入识别过程,不再接受语音输入 
  112.             showTip("结束说话"); 
  113.         } 
  114.   
  115.         @Override 
  116.         public void onResult(RecognizerResult results, boolean isLast) { 
  117. //          Log.d(TAG, results.getResultString()); 
  118.             printResult(results); 
  119.   
  120.             if (isLast) { 
  121.                 // TODO ***的结果 
  122.             } 
  123.         } 
  124.   
  125.         @Override 
  126.         public void onVolumeChanged(int volume, byte[] data) { 
  127.             showTip("当前正在说话,音量大小:" + volume); 
  128. //          Log.d(TAG, "返回音频数据:"+data.length); 
  129.         } 
  130.   
  131.         @Override 
  132.         public void onEvent(int eventType, int arg1, int arg2, Bundle obj) { 
  133.             // 以下代码用于获取与云端的会话id,当业务出错时将会话id提供给技术支持人员,可用于查询会话日志,定位出错原因 
  134.             // 若使用本地能力,会话id为null 
  135.             //  if (SpeechEvent.EVENT_SESSION_ID == eventType) { 
  136.             //      String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID); 
  137.             //      Log.d(TAG, "session id =" + sid); 
  138.             //  } 
  139.         } 
  140.     }; 
  141.       
  142.     /** 
  143.      * 听写UI监听器 
  144.      */ 
  145.     private RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() { 
  146.         public void onResult(RecognizerResult results, boolean isLast) { 
  147.             printResult(results); 
  148.         } 
  149.   
  150.         /** 
  151.          * 识别回调错误. 
  152.          */ 
  153.         public void onError(SpeechError error) { 
  154.             showTip(error.getPlainDescription(true)); 
  155.         } 
  156.   
  157.     }; 
  158.       
  159.     private void showTip(final String str) { 
  160.         mToast.setText(str); 
  161.         mToast.show(); 
  162.     } 
  163.       
  164.     private void printResult(RecognizerResult results) { 
  165.         String text = JsonParser.parseIatResult(results.getResultString()); 
  166.   
  167.         String sn = null
  168.         // 读取json结果中的sn字段 
  169.         try { 
  170.             JSONObject resultJson = new JSONObject(results.getResultString()); 
  171.             sn = resultJson.optString("sn"); 
  172.         } catch (JSONException e) { 
  173.             e.printStackTrace(); 
  174.         } 
  175.   
  176.         mIatResults.put(sn, text); 
  177.   
  178.         StringBuffer resultBuffer = new StringBuffer(); 
  179.         for (String key : mIatResults.keySet()) { 
  180.             resultBuffer.append(mIatResults.get(key)); 
  181.         } 
  182.   
  183.         mResult.setText(resultBuffer.toString()); 
  184. //      mResult.setSelection(mResultText.length()); 
  185.     } 
  186.       
  187.     /** 
  188.      * 初始化监听器。 
  189.      */ 
  190.     private InitListener mInitListener = new InitListener() { 
  191.   
  192.         @Override 
  193.         public void onInit(int code) { 
  194. //          Log.d(TAG, "SpeechRecognizer init() code = " + code); 
  195.             if (code != ErrorCode.SUCCESS) { 
  196. //              showTip("初始化失败,错误码:" + code); 
  197.             } 
  198.         } 
  199.     }; 
  200.   
  201.     /** 
  202.      * 参数设置 
  203.      *  
  204.      * @param param 
  205.      * @return 
  206.      */ 
  207.     public void setParam() { 
  208.         // 清空参数 
  209.         mIat.setParameter(SpeechConstant.PARAMS, null); 
  210.   
  211.         // 设置听写引擎 
  212.         mIat.setParameter(SpeechConstant.ENGINE_TYPE, mEngineType); 
  213.         // 设置返回结果格式 
  214.         mIat.setParameter(SpeechConstant.RESULT_TYPE, "json"); 
  215.   
  216.         String lag = mSharedPreferences.getString("iat_language_preference"
  217.                 "mandarin"); 
  218.         if (lag.equals("en_us")) { 
  219.             // 设置语言 
  220.             mIat.setParameter(SpeechConstant.LANGUAGE, "en_us"); 
  221.         } else { 
  222.             // 设置语言 
  223.             mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); 
  224.             // 设置语言区域 
  225.             mIat.setParameter(SpeechConstant.ACCENT, lag); 
  226.         } 
  227.   
  228.         // 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理 
  229.         mIat.setParameter(SpeechConstant.VAD_BOS, 
  230.                 mSharedPreferences.getString("iat_vadbos_preference""4000")); 
  231.   
  232.         // 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音 
  233.         mIat.setParameter(SpeechConstant.VAD_EOS, 
  234.                 mSharedPreferences.getString("iat_vadeos_preference""1000")); 
  235.   
  236.         // 设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点 
  237.         mIat.setParameter(SpeechConstant.ASR_PTT, 
  238.                 mSharedPreferences.getString("iat_punc_preference""1")); 
  239.   
  240.         // 设置音频保存路径,保存音频格式支持pcm、wav,设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限 
  241.         // 注:AUDIO_FORMAT参数语记需要更新版本才能生效 
  242.         mIat.setParameter(SpeechConstant.AUDIO_FORMAT, "wav"); 
  243.         mIat.setParameter(SpeechConstant.ASR_AUDIO_PATH, 
  244.                 Environment.getExternalStorageDirectory() + "/msc/iat.wav"); 
  245.   
  246.         // 设置听写结果是否结果动态修正,为“1”则在听写过程中动态递增地返回结果,否则只在听写结束之后返回最终结果 
  247.         // 注:该参数暂时只对在线听写有效 
  248.         mIat.setParameter(SpeechConstant.ASR_DWA, 
  249.                 mSharedPreferences.getString("iat_dwa_preference""0")); 
  250.     } 
  251.   
  252.     @Override 
  253.     public boolean onCreateOptionsMenu(Menu menu) { 
  254.         // Inflate the menu; this adds items to the action bar if it is present. 
  255.         getMenuInflater().inflate(R.menu.main, menu); 
  256.         return true
  257.     } 
  258.   
  259.     @Override 
  260.     public boolean onOptionsItemSelected(MenuItem item) { 
  261.         // Handle action bar item clicks here. The action bar will 
  262.         // automatically handle clicks on the Home/Up button, so long 
  263.         // as you specify a parent activity in AndroidManifest.xml. 
  264.         int id = item.getItemId(); 
  265.         if (id == R.id.action_settings) { 
  266.             return true
  267.         } 
  268.         return super.onOptionsItemSelected(item); 
  269.     } 
  270. </string,></string,> 

下载地址:http://down.51cto.com/data/2109303