Java實(shí)現(xiàn)在線語音識別
本文為大家分享了Java實(shí)現(xiàn)在線語音識別的具體方法,供大家參考,具體內(nèi)容如下
利用訊飛開發(fā)平臺作為第三方庫
首先需要在訊飛開發(fā)平臺下載SDK,網(wǎng)址為,訊飛開發(fā)平臺,這些SDK 下載都是免費(fèi)的,當(dāng)然你需要先注冊。在SDK 中不僅包含相應(yīng)的jar包,還有一些相應(yīng)的demo,可以供你參考學(xué)習(xí)
在我們下載下來第一個(gè)SDK 之后就可以進(jìn)行開發(fā)了,訊飛的SDK 給我們提供了詳盡而強(qiáng)大的函數(shù)支持,下面我就從代碼的角度來進(jìn)行一些解釋。
代碼
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(); // 設(shè)置組件 startBtn = new Button("start"); stopBtn = new Button("stop"); textArea = new TextArea(); Panel btnPanel = new Panel(); Panel textPanel = new Panel(); // Button startBtn = new Button("開始"); //添加監(jiān)聽器 startBtn.addActionListener(this); stopBtn.addActionListener(this); btnPanel.add(startBtn); btnPanel.add(stopBtn); textPanel.add(textArea); add(btnPanel); add(textPanel); // 設(shè)置窗體 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(); } } /** * 聽寫監(jiān)聽器 */ 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"); } /** * 獲取聽寫結(jié)果. 獲取RecognizerResult類型的識別結(jié)果,并對結(jié)果進(jìn)行累加,顯示到Area里 */ public void onResult(RecognizerResult results, boolean islast) { DebugLog.Log("onResult enter"); // 如果要解析json結(jié)果,請考本項(xiàng)目示例的 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"); } }; /** * 聽寫結(jié)束,恢復(fù)初始狀態(tài) */ 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類,語音識別類,語音識別,包括聽寫、語法識別功能。本類使用單例,調(diào)用者使用本類的對象,只需要通過createRecognizer()創(chuàng)建 一次對象后,便可一直使用該對象,直到通過調(diào)用destroy()進(jìn)行單例對象銷毀。調(diào) 用者可通過getRecognizer()獲取當(dāng)前已經(jīng)創(chuàng)建的單例。我們在一開始導(dǎo)包,把相應(yīng)的類導(dǎo)入,然后聲明語音識別類,然后在VoiceSpeech類的構(gòu)造器中初始化。
2.在SpeechRecognizer類中有很多有關(guān)語音識別的方法,
(1)startListening方法,開始進(jìn)行語音識別,其方法的參數(shù)是一個(gè)回調(diào)函數(shù),這個(gè)方法是另一個(gè)類RecognizerListener聲明的實(shí)例,在其匿名內(nèi)部類中重寫關(guān)鍵的方法,借此到底我們想要的結(jié)果,我們在onResult方法中重寫,把識別的結(jié)果通過json解析之后(識別的結(jié)果默認(rèn)是json格式),把它依次添加到文本欄上面,之后我們對文本欄的內(nèi)容進(jìn)行文字字體大小等的設(shè)定
(2)stopListening方法,等錄音結(jié)束之后,調(diào)用該方法,把錄音結(jié)果通過網(wǎng)絡(luò)傳輸給訊飛遠(yuǎn)程識別平臺進(jìn)行解析,解析完成之后,把解析結(jié)果傳送過來
3.在main方法中先要進(jìn)行SpeechUtility.createUtility,這是訊飛SDK的初始化,相當(dāng)于遠(yuǎn)程連接訊飛識別平臺,因?yàn)镴ava現(xiàn)在還不支持離線識別,所以在進(jìn)行識別方法調(diào)用之前,必須連接訊飛開發(fā)平臺,這個(gè)方法的作用正是如此,其參數(shù)就是不同的識別版本
4.因?yàn)楹芏喾椒ǘ际怯嶏w提供的,所以我們需要導(dǎo)入相應(yīng)的包
具體如下
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 中都有
最終的結(jié)果
ps:因?yàn)橹皇亲⒅刈R別功能,所以界面很丑。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于JDK源碼中的@author unascribed注釋閑談
這篇文章主要介紹了關(guān)于JDK源碼中的@author unascribed注釋閑談,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Java中Runnable與Callable接口的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Java中Runnable與Callable接口的區(qū)別,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2023-03-03BufferedWriter如何使用write方法實(shí)現(xiàn)換行
這篇文章主要介紹了BufferedWriter如何使用write方法實(shí)現(xiàn)換行的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java聊天室之實(shí)現(xiàn)運(yùn)行服務(wù)器與等待客戶端連接
這篇文章主要為大家詳細(xì)介紹了Java簡易聊天室之實(shí)現(xiàn)運(yùn)行服務(wù)器程序與等待客戶端程序連接功能,文中的示例代碼講解詳細(xì),需要的可以了解一下2022-10-10使用Java自帶的mail?API實(shí)現(xiàn)郵件發(fā)送功能全過程
電子郵件的應(yīng)用非常廣泛,例如在某網(wǎng)站注冊了一個(gè)賬戶,自動發(fā)送一封歡迎郵件,通過郵件找回密碼,自動批量發(fā)送活動信息等,下面這篇文章主要給大家介紹了關(guān)于如何使用Java自帶的mail?API實(shí)現(xiàn)郵件發(fā)送功能的相關(guān)資料,需要的朋友可以參考下2023-04-04Java中的日期時(shí)間類實(shí)例詳解(Date、Calendar、DateFormat)
在JDK1.0中,Date類是唯一的一個(gè)代表時(shí)間的類,但是由于Date類不便于實(shí)現(xiàn)國際化,所以從JDK1.1版本開始,推薦使用Calendar類進(jìn)行時(shí)間和日期處理,這篇文章主要介紹了Java中的日期時(shí)間類詳解(Date、Calendar、DateFormat),需要的朋友可以參考下2023-11-11Java將String字符串帶括號轉(zhuǎn)成List的簡單方法
Java中我們有時(shí)需要對現(xiàn)有的字符串進(jìn)行切割并轉(zhuǎn)化成一個(gè)List集合,這篇文章主要給大家介紹了關(guān)于Java將String字符串帶括號轉(zhuǎn)成List的簡單方法,需要的朋友可以參考下2023-03-03