科大讯飞 在线语音识别 音频来源为【文件】的java接入实现, 适用于初学者

版权声明:原创文章本人保留一切权力,转载必须联系我 企鹅号2110127392 https://blog.csdn.net/weixin_43112746/article/details/82665243
 ****科大讯飞的语音识别提供了两种音频来源方式,一个是通过麦克风,一个是来自音频文件。这里介绍本人自己写的通过音频
 文件识别的java代码。****

【离线识别参考我的另一篇】用java调用科大讯飞的离线语音识别dll实现离线识别(JNA实现)

之前的注册、获得注册码、以及SDK的下载这里不再赘述,直接上代码:

注意:
1、msc.jar一定要导入啊
2、本功能实现是在 在线情况下,离线情况下暂时用不了。

package com.iflytek;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.iflytek.cloud.speech.RecognizerListener;
import com.iflytek.cloud.speech.RecognizerResult;
import com.iflytek.cloud.speech.Setting;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechRecognizer;
import com.iflytek.cloud.speech.SpeechUtility;


public class VoiceTest {
	
	private static final String APPID="5a******1";//这里是自己的APPID
	private static VoiceTest mObject;
	private static StringBuilder mResult=new StringBuilder();
	private String fileName="test.pcm";//这里要将文件拷贝至根目录下,必须是.pcm文件
	
	//main方法,是否显示日志,语音实用程序验证程序的id
	public static void main(String[] args) {
		if(null!=args&&args.length>0&&args[0].equals("true")) {
			//显示日志
			Setting.setShowLog(true);
		}
		
		SpeechUtility.createUtility("appid="+APPID);
		getVoiceObj().Recognize();
	}
	
	//单例模式,创建对象
	private static VoiceTest getVoiceObj() {
		if(mObject==null) {
			mObject=new VoiceTest();
		}
		return mObject;
	}
	

	//建立语音识别对象
	private boolean mIsEndOfSpeech=false;
	private void Recognize() {
		if(SpeechRecognizer.getRecognizer()==null) {
			SpeechRecognizer.createRecognizer();
		}
		mIsEndOfSpeech=false;
		RecogizePcmFileBite();
	}

	//识别音频文件
	private void RecogizePcmFileBite() {
		//获取语音识别对象
		SpeechRecognizer recognizer=SpeechRecognizer.createRecognizer();
		//设置基本的识别参数,声音来源是音频,结果是自然语言文本
		recognizer.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");
		recognizer.setParameter(SpeechConstant.RESULT_TYPE, "plain");
		//开始监听,参数是监听器对象
		recognizer.startListening(recListener);
		//创建文件输入流
		FileInputStream fis=null;
		//创建字节数组,长度为64K
		byte[] data=new byte[64*1024];
		try {
			fis=new FileInputStream(new File("./"+fileName));
			//文件剩余长度如果没有,就显示没有了
			if(0==fis.available()) {
				mResult.append("no audio avaible!");
				//取消语音识别
				recognizer.cancel();
			//否则有语音文件
			}else {
				int len=data.length;//此时为64*1024即有这么长
				while(data.length==len&&!mIsEndOfSpeech) {
					//读取文件
					len=fis.read(data);
					//写出文件
					recognizer.writeAudio(data, 0, len);
				}
				//停止语音识别
				recognizer.stopListening();
			}
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(null !=fis) {
					fis.close();
					fis=null;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	//听写监听器
	private RecognizerListener recListener=new RecognizerListener() {
		@Override
		public void onBeginOfSpeech() {
			DebugLog.Log("onBeginOfSpeech enter");
			DebugLog.Log("*****开始录音*****");
			
		}
		
		@Override
		public void onVolumeChanged(int volume) {
			DebugLog.Log( "onVolumeChanged enter" );
			if (volume > 0)
				DebugLog.Log("*************音量值:" + volume + "*************");
			
		}
		
		@Override
		public void onResult(RecognizerResult result, boolean isLast) {
			DebugLog.Log( "onResult enter" );
			//获取监听结果的字符串
			mResult.append(result.getResultString());
			//如果是结尾
			if(isLast) {
				DebugLog.Log("识别结果为:"+mResult.toString());
				mIsEndOfSpeech=true;
				mResult.delete(0, mResult.length());
			}
			
		}
		
		@Override
		public void onEvent(int arg0, int arg1, int arg2, String arg3) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onError(SpeechError arg0) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onEndOfSpeech() {
			DebugLog.Log("onEndOfSpeech enter");
			DebugLog.Log("*****结束录音*****");
			mIsEndOfSpeech=true;
			
		}
		
	};
	
	

}

这里如果需要显示日志,记得把工具类粘上(里面的代码都不用动)

package com.iflytek;
import java.text.SimpleDateFormat;

public class DebugLog {
	
	public static void Log(String tag,String log)
	{
		if(true)
		    System.out.println(log);
	}
	
	public static void Log(String log)
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String date=dateFormat.format(new java.util.Date());
		if(true)
		    System.out.println("<" + date + ">" + log);
	}
	
	public static boolean isEmpty(String string){
		if(string == null)
		{
			return true;
		}
		if(string.isEmpty())
		{
			return true;
		}
		return false;
	}
}

最后直接run起来,就会将test.pcm这个音频文件的内容变为汉字输出到控制台
结果如下:

<2018-09-12 16:25:23>语音识别的结果是:汉堡包多少钱?一个英文怎么说?

test.pcm这个文件,SDK包里面有啊,不用问我要,在这

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43112746/article/details/82665243