好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实现在线语音识别

本文为大家分享了java实现在线 语音识别 的具体方法,供大家参考,具体内容如下

利用讯飞开发平台作为第三方库

首先需要在讯飞开发平台下载sdk,网址为, 讯飞开发平台 ,这些sdk 下载都是免费的,当然你需要先注册。在sdk 中不仅包含相应的jar包,还有一些相应的demo,可以供你参考学习

在我们下载下来第一个sdk 之后就可以进行开发了,讯飞的sdk 给我们提供了详尽而强大的函数支持,下面我就从代码的角度来进行一些解释。

代码

?

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

package myvoice;

import java.awt.button;

import java.awt.font;

import java.awt.frame;

import java.awt.gridlayout;

import java.awt.panel;

import java.awt.textarea;

import java.awt.event.actionevent;

import java.awt.event.actionlistener;

import java.lang.reflect.parameter;

import java.util.arraylist;

import javax.swing.imageicon;

import javax.swing.jframe;

import javax.swing.jlabel;

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

 

}

 

}

代码解析

1.speechrecognizer类,语音识别类,语音识别,包括听写、语法识别功能。本类使用单例,调用者使用本类的对象,只需要通过createrecognizer()创建 一次对象后,便可一直使用该对象,直到通过调用destroy()进行单例对象销毁。调 用者可通过getrecognizer()获取当前已经创建的单例。我们在一开始导包,把相应的类导入,然后声明语音识别类,然后在voicespeech类的构造器中初始化。

2.在speechrecognizer类中有很多有关语音识别的方法,

(1)startlistening方法,开始进行语音识别,其方法的参数是一个回调函数,这个方法是另一个类recognizerlistener声明的实例,在其匿名内部类中重写关键的方法,借此到底我们想要的结果,我们在onresult方法中重写,把识别的结果通过json解析之后(识别的结果默认是json格式),把它依次添加到文本栏上面,之后我们对文本栏的内容进行文字字体大小等的设定

(2)stoplistening方法,等录音结束之后,调用该方法,把录音结果通过网络传输给讯飞远程识别平台进行解析,解析完成之后,把解析结果传送过来

3.在main方法中先要进行speechutility.createutility,这是讯飞sdk的初始化,相当于远程连接讯飞识别平台,因为java现在还不支持离线识别,所以在进行识别方法调用之前,必须连接讯飞开发平台,这个方法的作用正是如此,其参数就是不同的识别版本

4.因为很多方法都是讯飞提供的,所以我们需要导入相应的包

具体如下

?

1

2

3

4

5

6

7

8

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; //json解析类

import com.iflytek.util.version; //版本类

这些在sdk 中都有

最终的结果

ps:因为只是注重识别功能,所以界面很丑。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_33945246/article/details/79665010

查看更多关于Java实现在线语音识别的详细内容...

  阅读:40次