最近想用第三方api做点东西玩,一直在网上找不到,讯飞的java版的demo,就自己写了一个供大家参考,比较简陋,没有界面,运行main函数,说话就行了,
这个工程师api自带的,test的类是我的Demo
package com.iflytek;
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;
public class VoiceTest {
SpeechRecognizer speechRecognizer;
String resultText = "";
public VoiceTest(){
this.speechRecognizer = SpeechRecognizer.createRecognizer();
}
public void speech(){
if(!speechRecognizer.isListening())
speechRecognizer.startListening(listener);
else
speechRecognizer.stopListening();
}
public String text(){
return resultText;
}
//编写监听器,创建匿名内部类
private RecognizerListener listener = new RecognizerListener() {
/**
* 当在说话的过程中音量发生变化时会多次调用此函数,显示音量值
*/
@Override
public void onVolumeChanged(int arg0) {
DebugLog.Log("onVolumeChanged enter "+arg0);
}
/**
* 获取听写结果. 获取RecognizerResult类型的识别结果
*/
@Override
public void onResult(RecognizerResult result, boolean flag) {
DebugLog.Log("onResult enter");
//这个result就是远程解析的结果
String strResult = null;
//这里的捕获异常是我自己修改了JsonParser的这个静态方法,因为他在里面捕获了异常,所以我修改了,我在那里面又抛了一个异常
//因为这个函数解析result的时候,如果不说话就会打印异常信息,所以受不了,我就把他修改了
try{
strResult = JsonParser.parseIatResult(result.getResultString());
}catch(Exception e){
strResult = "";
}
resultText = result.getResultString();
System.out.println(strResult);
}
/*
* 事件 扩展用接口,由具体业务进行约定
* @see com.iflytek.cloud.speech.RecognizerListener#onEvent(int, int, int, java.lang.String)
*/
@Override
public void onEvent(int arg0, int arg1, int arg2, String arg3) {
}
@Override
public void onError(SpeechError arg0) {
DebugLog.Log("onError enter");
}
@Override
public void onEndOfSpeech() {
DebugLog.Log("onEndOfSpeech enter");
}
/*
* 结束听写,恢复初始状态
* @see com.iflytek.cloud.speech.RecognizerListener#onBeginOfSpeech()
*/
@Override
public void onBeginOfSpeech() {
DebugLog.Log("onBeginOfSpeech enter");
}
};
public static void main(String[] args) {
//这句是必须的,注册的时候必须建一个应用,会分配一个appid
SpeechUtility.createUtility("appid=59b78833");
//初始化这个类的时候,这些函数就调用了,如果不初始化,那个匿名内部类就没办法监听,监听器的方法有的需要重写,有的可以不重写
VoiceTest speechTest = new VoiceTest();
//新版api当startListening被调用之后,自动判断音量为静音来自动停止,所以不需要关心停止的
speechTest.speech();
//System.out.println(speechTest.text());
}
}
-----------------------------------------------------------------------------------------------------------------
看到大家点评问debugLog和JsonParser的类没有,这个也是在官网下的sdk,还有项目中要导入一个jar包,也在这个sdk中,教程:
https://doc.xfyun.cn/msc_java/%E9%9B%86%E6%88%90%E8%AF%B4%E6%98%8E.html
sdk下载是需要登陆的,下载地址
https://www.xfyun.cn/sdk/dispatcher
如果觉得麻烦,我上传了util:
https://download.csdn.net/download/chunlaiqingke/11174494
(ps:这个金币数不是我能控制的,不能改为0,如果谁知道,点评告知一下)
========================================================================================
我发现大家好多是代码小白,写成这样有人还是不会引包和找到util,甚至连java项目都不会。
idea创建java项目:https://blog.csdn.net/oschina_41790905/article/details/79475187
eclipse创建java项目:https://www.cnblogs.com/msdog/p/9349349.html
首先,按照上面的sdk下载地址下载,他会要求你登录,你注册登录一下,然后选java
这样下载完的压缩包里面就有DebugLog和JsonParser了(在sample目录下),
把他们拷贝出来放到util目录下,文件目录结构按照我的来。
还有2个重要的包需要引入(json-jena-1.0.jar和Msc.jar),也在那个下载的sdk压缩包下面,这2个jar包的引入方式不同的ide不一样
eclipse版:https://www.xfyun.cn/doc/asr/voicedictation/Java-SDK.html#%E9%9B%86%E6%88%90%E8%AF%B4%E6%98%8E
idea版:https://www.cnblogs.com/zheting/p/7748512.html
如果还有疑问,可以在这点评或者在公众号上留言,或者我邮箱
微信公众号