用科大讯飞的云服务来实现java语音识别,把说的话转化成一句话文字并输出
点击下列链接来获取源项目
1.如果要自己写项目的话,复制最下面的代码,并且要把上图中com.iflytek.util包 包含上,并且复制lib和msc文件夹还有msc下面的四个文件
2.之后右击项目属性,选择Java Build Path->Libraries->Add JARs->然后把lib文件夹中的json.jar和msc.jar选择上
扫描二维码关注公众号,回复:
250416 查看本文章
->之后Apply->然后在编译就可以了
代码如下:
package sound; import com.iflytek.cloud.speech.RecognizerListener; import com.iflytek.cloud.speech.RecognizerResult; import com.iflytek.cloud.speech.SpeechError; import com.iflytek.cloud.speech.SpeechRecognizer; import com.iflytek.cloud.speech.SpeechUtility; import com.iflytek.util.DebugLog; import com.iflytek.util.JsonParser; import com.iflytek.util.Version; public class textXunFei { // 语音听写对象 SpeechRecognizer speechRecognize; public textXunFei() { // 初始化听写对象 speechRecognize = SpeechRecognizer.createRecognizer(); } //开始监听并向讯飞服务器发送语音 public int startListen() { if (!speechRecognize.isListening()) speechRecognize.startListening(mRecoListener); else speechRecognize.stopListening(); return 0; } /** * 监听器 */ private RecognizerListener mRecoListener = new RecognizerListener(){ //获取结果 public void onResult(RecognizerResult results, boolean isLast){ //用json来获取结果 String text = results.getResultString(); JsonParser json = new JsonParser(); String newTest = json.parseIatResult(text); System.out.print(newTest); } //会话发生错误回调接口 public void onError(SpeechError error) { //error.getPlainDescription(true); //获取错误码描述 } //开始录音 public void onBeginOfSpeech() {} //音量值0~30 public void onVolumeChanged(int volume){} //结束录音 public void onEndOfSpeech() {} //扩展用接口 public void onEvent(int eventType,int arg1,int arg2,String msg) {} }; public static void main(String[] args) { //初始化听写对象 textXunFei t=new textXunFei(); StringBuffer param=new StringBuffer(); param = new StringBuffer(); param.append( "appid=" + Version.getAppid() ); SpeechUtility.createUtility(param.toString() ); t.startListen(); } }
原创文章,禁止转载
更多请访问科大讯飞官网