在Java中实现在线语音识别

我会带着你远行 2021-09-22 10:58 472阅读 0赞

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

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

讯飞开发平台

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

代码

  1. package myVoice;
  2. import java.awt.Button;
  3. import java.awt.Font;
  4. import java.awt.Frame;
  5. import java.awt.GridLayout;
  6. import java.awt.Panel;
  7. import java.awt.TextArea;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.lang.reflect.Parameter;
  11. import java.util.ArrayList;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import com.iflytek.cloud.speech.RecognizerListener;
  16. import com.iflytek.cloud.speech.RecognizerResult;
  17. import com.iflytek.cloud.speech.SpeechError;
  18. import com.iflytek.cloud.speech.SpeechRecognizer;
  19. import com.iflytek.cloud.speech.SpeechUtility;
  20. import com.iflytek.util.DebugLog;
  21. import com.iflytek.util.JsonParser;
  22. import com.iflytek.util.Version;
  23. public class VoiceSpeech extends Frame implements ActionListener {
  24. Button startBtn;
  25. Button stopBtn;
  26. TextArea textArea;
  27. // 语音听写对象
  28. SpeechRecognizer speechRecognize;
  29. private static final String DEF_FONT_NAME = "宋体";
  30. private static final int DEF_FONT_STYLE = Font.BOLD;
  31. private static final int DEF_FONT_SIZE = 30;
  32. private static final int TEXT_COUNT = 100;
  33. public VoiceSpeech() {
  34. // 初始化听写对象
  35. speechRecognize = SpeechRecognizer.createRecognizer();
  36. // 设置组件
  37. startBtn = new Button("start");
  38. stopBtn = new Button("stop");
  39. textArea = new TextArea();
  40. Panel btnPanel = new Panel();
  41. Panel textPanel = new Panel();
  42. // Button startBtn = new Button("开始");
  43. //添加监听器
  44. startBtn.addActionListener(this);
  45. stopBtn.addActionListener(this);
  46. btnPanel.add(startBtn);
  47. btnPanel.add(stopBtn);
  48. textPanel.add(textArea);
  49. add(btnPanel);
  50. add(textPanel);
  51. // 设置窗体
  52. setLayout(new GridLayout(2, 1));
  53. setSize(400, 300);
  54. setTitle("语音识别");
  55. setLocation(200, 200);
  56. setVisible(true);
  57. }
  58. public void actionPerformed(ActionEvent e) {
  59. if (e.getSource() == startBtn) {
  60. textArea.setText("*************你说的是:");
  61. if (!speechRecognize.isListening())
  62. speechRecognize.startListening(recognizerListener);
  63. else
  64. speechRecognize.stopListening();
  65. } else if (e.getSource() == stopBtn) {
  66. speechRecognize.stopListening();
  67. }
  68. }
  69. /** * 听写监听器 */
  70. private RecognizerListener recognizerListener = new RecognizerListener() {
  71. public void onBeginOfSpeech() {
  72. // DebugLog.Log( "onBeginOfSpeech enter" );
  73. // ((JLabel) jbtnRecognizer.getComponent(0)).setText("听写中...");
  74. // jbtnRecognizer.setEnabled(false);
  75. }
  76. public void onEndOfSpeech() {
  77. DebugLog.Log("onEndOfSpeech enter");
  78. }
  79. /** * 获取听写结果. 获取RecognizerResult类型的识别结果,并对结果进行累加,显示到Area里 */
  80. public void onResult(RecognizerResult results, boolean islast) {
  81. DebugLog.Log("onResult enter");
  82. // 如果要解析json结果,请考本项目示例的 com.iflytek.util.JsonParser类
  83. String text =
  84. JsonParser.parseIatResult(results.getResultString());
  85. // String text = results.getResultString();
  86. // JsonParser json = new JsonParser();
  87. // String newTest = json.parseIatResult(text);
  88. // textArea.setText(newTest);
  89. textArea.append(text);
  90. text = textArea.getText();
  91. if (null != text) {
  92. int n = text.length() / TEXT_COUNT + 1;
  93. int fontSize = Math.max(10, DEF_FONT_SIZE - 2 * n);
  94. DebugLog.Log("onResult new font size=" + fontSize);
  95. int style = n > 1 ? Font.PLAIN : DEF_FONT_SIZE;
  96. Font newFont = new Font(DEF_FONT_NAME, style, fontSize);
  97. textArea.setFont(newFont);
  98. }
  99. if (islast) {
  100. iatSpeechInitUI();
  101. }
  102. }
  103. public void onVolumeChanged(int volume) {
  104. DebugLog.Log("onVolumeChanged enter");
  105. if (volume == 0)
  106. volume = 1;
  107. else if (volume >= 6)
  108. volume = 6;
  109. // labelWav.setIcon(new ImageIcon("res/mic_0" + volume + ".png"));
  110. }
  111. public void onError(SpeechError error) {
  112. DebugLog.Log("onError enter");
  113. if (null != error) {
  114. DebugLog.Log("onError Code:" + error.getErrorCode());
  115. textArea.setText(error.getErrorDescription(true));
  116. iatSpeechInitUI();
  117. }
  118. }
  119. public void onEvent(int eventType, int arg1, int agr2, String msg) {
  120. DebugLog.Log("onEvent enter");
  121. }
  122. };
  123. /** * 听写结束,恢复初始状态 */
  124. public void iatSpeechInitUI() {
  125. // labelWav.setIcon(new ImageIcon("res/mic_01.png"));
  126. // jbtnRecognizer.setEnabled(true);
  127. // ((JLabel) jbtnRecognizer.getComponent(0)).setText("开始听写");
  128. }
  129. public static void main(String[] args) {
  130. // 初始化
  131. StringBuffer param = new StringBuffer();
  132. param.append( "appid=" + Version.getAppid() );
  133. // param.append( ","+SpeechConstant.LIB_NAME_32+"=myMscName" );
  134. SpeechUtility.createUtility( param.toString() );
  135. VoiceSpeech t = new VoiceSpeech();
  136. }
  137. }

代码解析

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

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

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

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

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

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

具体如下

  1. import com.iflytek.cloud.speech.RecognizerListener;
  2. import com.iflytek.cloud.speech.RecognizerResult;
  3. import com.iflytek.cloud.speech.SpeechError;
  4. import com.iflytek.cloud.speech.SpeechRecognizer;
  5. import com.iflytek.cloud.speech.SpeechUtility;
  6. import com.iflytek.util.DebugLog;
  7. import com.iflytek.util.JsonParser;//json解析类
  8. import com.iflytek.util.Version;//版本类

这些在SDK 中都有

最终的结果

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

最终截图

发表评论

表情:
评论列表 (有 0 条评论,472人围观)

还没有评论,来说两句吧...

相关阅读

    相关 语音识别系统

    语音识别系统 -------------------- 本系统旨在构建一个语音识别系统,实现电脑自动将人类的语音内容识别出来。 -------------------

    相关 Python实现语音识别

    案例分析 概述            Python在语音识别方面功能很强大,程序语言简单高效,下面编程实现一下如何实现语音识别。本文实现案例是将文本转换成语音,给出

    相关 Python 语音识别

    > 调用科大讯飞语音听写,使用`Python`实现语音识别,将实时语音转换为文字。 参考这篇[博客][Link 1]实现的录音,首先在官网下载了关于语音听写的`SDK`,然后