java语音识别(科大讯飞版)

用科大讯飞的云服务来实现java语音识别,把说的话转化成一句话文字并输出java


点击下列连接来获取源项目git

点击打开项目代码github




1.若是要本身写项目的话,复制最下面的代码,而且要把上图中com.iflytek.util包 包含上,而且复制lib和msc文件夹还有msc下面的四个文件json



2.以后右击项目属性,选择Java Build Path->Libraries->Add JARs->而后把lib文件夹中的json.jar和msc.jar选择上服务器

->以后Apply->而后在编译就能够了app


代码以下:ui

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();
	}
}
 
 

原创文章,禁止转载spa

更多请访问科大讯飞官网code

科大讯飞语音听写介绍
对象

科大讯飞语音听写教程文档