1.前言
为了方便人们使用,现在需要输入的形式也越来越少了,想着试用以下大模型下的语言识别,找到了讯飞开发平台也有免费的试用活动。
效果截图:

2. 注册
账号注册等等跳过,寻找下列的实时语音转写:

选择免费的试用套餐(需要实名认证):

在完成免费的购买后,回到首页选择流式版:

记住其中的id和点击下部分的下载java MSC

选择普通版本下载:

下载后的解压文件如下:

3.创建项目
首先创建一个空的Maven项目,在其中创建两个空的文件夹目录:

VoiceSpeech.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| package com.example.xunfei.voice;
import com.iflytek.cloud.speech.*; import com.example.xunfei.util.DebugLog; import com.example.xunfei.util.JsonParser; import com.example.xunfei.util.Version;
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class VoiceSpeech extends Frame implements ActionListener {
Button startBtn;
Button stopBtn;
TextArea textArea;
SpeechRecognizer speechRecognize;
private static final String DEF_FONT_NAME = "宋体";
private static final int DEF_FONT_STYLE = Font.BOLD;
private static final int DEF_FONT_SIZE = 30;
private static final int TEXT_COUNT = 100;
public VoiceSpeech() {
speechRecognize = SpeechRecognizer.createRecognizer();
startBtn = new Button("start");
stopBtn = new Button("stop");
textArea = new TextArea();
Panel btnPanel = new Panel();
Panel textPanel = new Panel();
startBtn.addActionListener(this);
stopBtn.addActionListener(this);
btnPanel.add(startBtn);
btnPanel.add(stopBtn);
textPanel.add(textArea);
add(btnPanel);
add(textPanel);
setLayout(new GridLayout(2, 1));
setSize(400, 300);
setTitle("语音识别");
setLocation(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startBtn) {
textArea.setText("你说的是:");
if (!speechRecognize.isListening())
speechRecognize.startListening(recognizerListener);
else
speechRecognize.stopListening();
} else if (e.getSource() == stopBtn) {
speechRecognize.stopListening();
}
}
private RecognizerListener recognizerListener = new RecognizerListener() {
public void onBeginOfSpeech() {
}
public void onEndOfSpeech() {
DebugLog.Log("onEndOfSpeech enter");
}
public void onResult(RecognizerResult results, boolean islast) {
DebugLog.Log("onResult enter");
String text =
JsonParser.parseIatResult(results.getResultString());
textArea.append(text);
text = textArea.getText();
if (null != text) {
int n = text.length() / TEXT_COUNT + 1;
int fontSize = Math.max(10, DEF_FONT_SIZE - 2 * n);
DebugLog.Log("onResult new font size=" + fontSize);
int style = n > 1 ? Font.PLAIN : DEF_FONT_SIZE;
Font newFont = new Font(DEF_FONT_NAME, style, fontSize);
textArea.setFont(newFont);
}
if (islast) {
iatSpeechInitUI();
}
}
public void onVolumeChanged(int volume) {
DebugLog.Log("onVolumeChanged enter");
if (volume == 0)
volume = 1;
else if (volume >= 6)
volume = 6;
}
public void onError(SpeechError error) {
DebugLog.Log("onError enter");
if (null != error) {
DebugLog.Log("onError Code:" + error.getErrorCode());
textArea.setText(error.getErrorDescription(true));
iatSpeechInitUI();
}
}
public void onEvent(int eventType, int arg1, int agr2, String msg) {
DebugLog.Log("onEvent enter");
}
};
public void iatSpeechInitUI() {
}
public static void main(String[] args) {
StringBuffer param = new StringBuffer();
param.append("appid=" + Version.getAppid());
SpeechUtility.createUtility(param.toString());
VoiceSpeech t = new VoiceSpeech();
}
}
|
此时或有很多的错误,因为我们没有导入对应的包,我们找到之前下载中下载好的SDK文件夹下,进入下面的lib–>lib目录下,找到两个jar包。


然后将两个jar包导入到项目中:

点击ok发现com.xxx.cloud.speech相关的不爆红了,但是com.xxx.util相关的import仍然会爆红,所以我这里下一步是选择在com目录下手动新建util包【使其能够手动导入】之后找到下载解压后的SDK文件夹中的sample
跟着目录找到sample–>src–>com–>iflytek–>util下的6个类,全选,复制粘贴到我们本地IDEA的对应包com.xxx.util下。

此时应该所有的项目错误都应该解决了。
注意查看一下com.xxx.util.Version中的getAppid方法返回值,为自己的Appid

最后复制我们SDK中的.so和.dll文件一共4个到项目根目录下


4. 运行项目
找到VoiceSpeech第232行左右的main函数执行。

