讯飞实时转写模型

1.前言

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

效果截图:

image-20241223212110425

2. 注册

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

image-20241223210206914

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

image-20241223210348970

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

image-20241223210510006

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

image-20241223210629114

选择普通版本下载:

image-20241223210712061

下载后的解压文件如下:

image-20241223210747129

3.创建项目

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

image-20241223210942684

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

// Button startBtn = new Button("开始");

//添加监听器

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() {

// DebugLog.Log( "onBeginOfSpeech enter" );

// ((JLabel) jbtnRecognizer.getComponent(0)).setText("听写中...");

// jbtnRecognizer.setEnabled(false);

}

public void onEndOfSpeech() {

DebugLog.Log("onEndOfSpeech enter");

}

/**
* 获取听写结果. 获取RecognizerResult类型的识别结果,并对结果进行累加,显示到Area里

*/

public void onResult(RecognizerResult results, boolean islast) {

DebugLog.Log("onResult enter");

// 如果要解析json结果,请考本项目示例的 com.iflytek.util.JsonParser类

String text =

JsonParser.parseIatResult(results.getResultString());

// String text = results.getResultString();

// JsonParser json = new JsonParser();

// String newTest = json.parseIatResult(text);

// textArea.setText(newTest);

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;

// labelWav.setIcon(new ImageIcon("res/mic_0" + volume + ".png"));

}

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() {

// labelWav.setIcon(new ImageIcon("res/mic_01.png"));

// jbtnRecognizer.setEnabled(true);

// ((JLabel) jbtnRecognizer.getComponent(0)).setText("开始听写");

}

public static void main(String[] args) {

// 初始化

StringBuffer param = new StringBuffer();

param.append("appid=" + Version.getAppid());

// param.append( ","+SpeechConstant.LIB_NAME_32+"=myMscName" );

SpeechUtility.createUtility(param.toString());

VoiceSpeech t = new VoiceSpeech();

}

}

此时或有很多的错误,因为我们没有导入对应的包,我们找到之前下载中下载好的SDK文件夹下,进入下面的lib–>lib目录下,找到两个jar包。

image-20241223211146867

image-20241223211202402

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

image-20241223211233107

点击ok发现com.xxx.cloud.speech相关的不爆红了,但是com.xxx.util相关的import仍然会爆红,所以我这里下一步是选择在com目录下手动新建util包【使其能够手动导入】之后找到下载解压后的SDK文件夹中的sample

跟着目录找到sample–>src–>com–>iflytek–>util下的6个类,全选,复制粘贴到我们本地IDEA的对应包com.xxx.util下。

image-20241223211431011

此时应该所有的项目错误都应该解决了。

注意查看一下com.xxx.util.Version中的getAppid方法返回值,为自己的Appid

image-20241223211627369

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

image-20241223211739642

image-20241223211839888

4. 运行项目

找到VoiceSpeech第232行左右的main函数执行。

image-20241223211905604

image-20241223212110425