一、下載語(yǔ)音聽(tīng)寫(xiě)(流式版)SDK
科大訊飛官網(wǎng):https://www.xfyun.cn/
1.1 實(shí)名認(rèn)證
首先登陸訊飛開(kāi)放平臺(tái):https://passport.xfyun.cn/login,微信掃碼關(guān)注登錄
注冊(cè)新賬號(hào)
登陸后界面后,進(jìn)入產(chǎn)品服務(wù)–>實(shí)時(shí)語(yǔ)音轉(zhuǎn)寫(xiě)欄目
點(diǎn)擊個(gè)人免費(fèi)套餐,下面的立即領(lǐng)取,它會(huì)提醒我們?nèi)?shí)名認(rèn)證
實(shí)名認(rèn)證一下
提交完認(rèn)證之后
可以看到認(rèn)證成功
回到平臺(tái)領(lǐng)取界面,就可以領(lǐng)取了
1.2 創(chuàng)建應(yīng)用并試用免費(fèi)購(gòu)買(mǎi)版
點(diǎn)擊右邊的+號(hào)創(chuàng)建應(yīng)用,很簡(jiǎn)單的,然后才能提交(不然會(huì)提示你還沒(méi)有創(chuàng)建應(yīng)用,不讓提交)
確認(rèn)下單
設(shè)置下支付密碼
確認(rèn)支付就好了
1.3 下載SDK
在控制臺(tái)進(jìn)入后有如下界面,點(diǎn)擊語(yǔ)音聽(tīng)寫(xiě),往下翻就可以找到Java MSC,點(diǎn)擊下載就好了
紅色箭頭指向的是我們上一步創(chuàng)建的的項(xiàng)目名稱
解壓后目錄如下:
1.4 一般我們使用SDK調(diào)用方式的話,只需要用到APPID。
在本地IDEA項(xiàng)目中使用的話,使用的是自己項(xiàng)目中下載的SDK包
,和自己官網(wǎng)的Appid
。否則SDK包和Appid不對(duì)應(yīng)的話會(huì)報(bào)錯(cuò)
二、使用IDEA建立項(xiàng)目并實(shí)現(xiàn)【一定要使用自己官網(wǎng)的SDK和Appid對(duì)應(yīng),否則會(huì)出錯(cuò)10407】
2.1 在IDEA中新建Maven項(xiàng)目
2.2 在java下新建com.zhj.voice包,寫(xiě)入VoiceSpeech類,導(dǎo)入MSC的jar包
VoiceSpeech類完整代碼如下:【注意導(dǎo)入的各個(gè)包名】
package com.zhj.voice;
/**
* Topic
* Description
*
* @author zhouh
* @version 1.0
* Create by 2022/8/3 10:58
*/
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;
// 語(yǔ)音聽(tīng)寫(xiě)對(duì)象
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() {
// 初始化聽(tīng)寫(xiě)對(duì)象
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("開(kāi)始");
//添加監(jiān)聽(tīng)器
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("語(yǔ)音識(shí)別");
setLocation(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startBtn) {
textArea.setText("*************你說(shuō)的是:");
if (!speechRecognize.isListening())
speechRecognize.startListening(recognizerListener);
else
speechRecognize.stopListening();
} else if (e.getSource() == stopBtn) {
speechRecognize.stopListening();
}
}
/**
* 聽(tīng)寫(xiě)監(jiān)聽(tīng)器
*/
private RecognizerListener recognizerListener = new RecognizerListener() {
public void onBeginOfSpeech() {
// DebugLog.Log( "onBeginOfSpeech enter" );
// ((JLabel) jbtnRecognizer.getComponent(0)).setText("聽(tīng)寫(xiě)中...");
// jbtnRecognizer.setEnabled(false);
}
public void onEndOfSpeech() {
DebugLog.Log("onEndOfSpeech enter");
}
/**
* 獲取聽(tīng)寫(xiě)結(jié)果. 獲取RecognizerResult類型的識(shí)別結(jié)果,并對(duì)結(jié)果進(jìn)行累加,顯示到Area里
*/
public void onResult(RecognizerResult results, boolean islast) {
DebugLog.Log("onResult enter");
// 如果要解析json結(jié)果,請(qǐng)考本項(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");
}
};
/**
* 聽(tīng)寫(xiě)結(jié)束,恢復(fù)初始狀態(tài)
*/
public void iatSpeechInitUI() {
// labelWav.setIcon(new ImageIcon("res/mic_01.png"));
// jbtnRecognizer.setEnabled(true);
// ((JLabel) jbtnRecognizer.getComponent(0)).setText("開(kāi)始聽(tīng)寫(xiě)");
}
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();
}
}
接著可能會(huì)有包名爆紅,提醒我們導(dǎo)入Jar包
我們找到1.3中下載好的SDK文件夾下,進(jìn)入下面的lib–>lib目錄下,找到兩個(gè)jar包。【注意,Java_iat1021_a8641a01 (1)是我下載的SDK解壓后的名字】
然后將兩個(gè)jar包導(dǎo)入到項(xiàng)目中:
點(diǎn)擊ok發(fā)現(xiàn)com.iflytek.cloud.speech
相關(guān)的不爆紅了
但是com.iflytek.util
相關(guān)的import仍然會(huì)爆紅,所以我這里下一步是選擇在com目錄下手動(dòng)新建iflytek.util包【使其能夠手動(dòng)導(dǎo)入】
2.3 手動(dòng)新建iflytek.util包,復(fù)制導(dǎo)入文件
但是com.iflytek.util
相關(guān)的import仍然會(huì)爆紅,所以我這里是選擇在com目錄下手動(dòng)新建iflytek.util包【使其能夠手動(dòng)導(dǎo)入】
之后找到1.2步下載解壓后的SDK文件夾中的sample
跟著目錄找到sample–>src–>com–>iflytek–>util下的6個(gè)類
全選,復(fù)制粘貼到我們本地IDEA的對(duì)應(yīng)包c(diǎn)om.iflytek.util下
(這里包下Version類名中顯示藍(lán)色,是因?yàn)槲乙呀?jīng)上傳到github上
并且本地IDEA修改代碼了
,所以會(huì)顯示藍(lán)色)
這時(shí)候會(huì)發(fā)現(xiàn)不報(bào)錯(cuò)了,所有的import都正常顯示了
2.4 修改com.iflytek.util.Version類中的getAppid方法返回值,為自己的Appid
修改com.iflytek.util.Version類中的getAppid方法返回值為我們科大訊飛官網(wǎng)中項(xiàng)目的Appid,因?yàn)榉祷?code>String類型,記得Appid加雙引號(hào):
"自己的Appid號(hào)"
Appid號(hào)在1.4節(jié)的時(shí)候得到了:
2.5 復(fù)制我們SDK中的.so和.dll文件一共4個(gè)到項(xiàng)目根目錄下
在本地下載解壓好的SDK問(wèn)價(jià)夾中找到lib–>lib包下的這4個(gè)文件,Ctrl+A后,Ctrl+C全選復(fù)制
然后粘貼到本地IDEA的項(xiàng)目根目錄下就好了
完整工程目錄如下:
至此,項(xiàng)目搭建就完成了。
三、啟動(dòng)項(xiàng)目
進(jìn)入VoiceSpeech類中運(yùn)行main函數(shù)就可以成功啟動(dòng)項(xiàng)目且不報(bào)錯(cuò)了:
運(yùn)行后會(huì)彈出彈框,點(diǎn)擊start說(shuō)話就可以識(shí)別到了。
識(shí)別后想要再次說(shuō)話識(shí)別,點(diǎn)擊stop后再點(diǎn)擊start就可以了
四、常見(jiàn)報(bào)錯(cuò)
-
使用科大飛訊語(yǔ)音合成報(bào) 20021 引擎錯(cuò)誤
:原因和解決參考我的這一篇博客使用科大飛訊語(yǔ)音合成SDK報(bào) 20021 引擎錯(cuò)誤 - 關(guān)
于訊飛科大導(dǎo)入的10407問(wèn)題
:原因(沒(méi)復(fù)制.so和.ddl文件)和解決辦法參考我的這一篇博客關(guān)于訊飛科大語(yǔ)音識(shí)別SDK導(dǎo)入的10407問(wèn)題
參考:在Java中實(shí)現(xiàn)在線語(yǔ)音識(shí)別文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-419832.html
Idea導(dǎo)入jar包的兩種方法文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-419832.html
到了這里,關(guān)于Java中實(shí)現(xiàn)在線語(yǔ)音識(shí)別(科大訊飛免費(fèi)的SKD)、SDK下載和IDEA項(xiàng)目搭建、成功運(yùn)行【完整代碼】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!