各位看官可以關注博主個人博客,了解更多信息。
作者:Surpasser
鏈接地址:https://surpass.org.cn
前言
博主的畢設系統在做一個餐廳的點餐管理系統,在記性移動端頁面開發的時候突發奇想做一個呼叫服務員,揚聲器發聲的一個功能類似於:“工作人員請注意,桌號8001顧客正在尋求幫助!”。
實現方式
接下來就對這個小功能進行分析和實現。先寫一個Demo。
-
首先,我們需要一個dll作為輔助。這里解釋一下dll的含義(DLL(Dynamic Link Library)文件為動態鏈接庫文件,又稱“應用百程序拓展”,是軟件文件類型。在Windows中,許多應用程序並不是一個度完整的可執行文件,它們被分割成一些相知對獨立的動態鏈接庫,即DLL文件,放置於道系統中。當我們執行某一個程序時,相應的版DLL文件就會被調用。一個應用程序可使用權多個DLL文件,一個DLL文件也可能被不同的應用程序使用,這樣的DLL文件被稱為共享DLL文件)。
需要把
jacob-1.17-M2-x64.dll
復制到C:\Windows\System32\
目錄下。我們也能看到目錄下有很多的.dll
文件。這里的文件大家自己百度下,很好找的。
-
使用maven項目導入坐標。
<!-- https://mvnrepository.com/artifact/net.sf.jacob-project/jacob --> <dependency> <groupId>net.sf.jacob-project</groupId> <artifactId>jacob</artifactId> <version>1.14.3</version> </dependency>
-
測試類代碼。
/** * 文字轉語音測試 jdk bin文件中需要導入jacob-1.17-M2-x64.dll * 注意導包哈 * @date: 2020年2月25日 上午10:05:21 */ public class Jacobtest { public static void main(String[] args) { textToSpeech("工作人員請注意,桌號8001顧客正在尋求幫助!!"); } /** * 語音轉文字並播放 * * @param text */ public static void textToSpeech(String text) { ActiveXComponent ax = null; try { ax = new ActiveXComponent("Sapi.SpVoice"); // 運行時輸出語音內容 Dispatch spVoice = ax.getObject(); // 音量 0-100 ax.setProperty("Volume", new Variant(100)); // 語音朗讀速度 -10 到 +10 ax.setProperty("Rate", new Variant(0)); // 執行朗讀 Dispatch.call(spVoice, "Speak", new Variant(text)); /* // 下面是構建文件流把生成語音文件 ax = new ActiveXComponent("Sapi.SpFileStream"); Dispatch spFileStream = ax.getObject(); ax = new ActiveXComponent("Sapi.SpAudioFormat"); Dispatch spAudioFormat = ax.getObject(); // 設置音頻流格式 Dispatch.put(spAudioFormat, "Type", new Variant(22)); // 設置文件輸出流格式 Dispatch.putRef(spFileStream, "Format", spAudioFormat); // 調用輸出 文件流打開方法,創建一個.wav文件 Dispatch.call(spFileStream, "Open", new Variant("./text.wav"), new Variant(3), new Variant(true)); // 設置聲音對象的音頻輸出流為輸出文件對象 Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream); // 設置音量 0到100 Dispatch.put(spVoice, "Volume", new Variant(100)); // 設置朗讀速度 Dispatch.put(spVoice, "Rate", new Variant(-2)); // 開始朗讀 Dispatch.call(spVoice, "Speak", new Variant(text)); // 關閉輸出文件 Dispatch.call(spFileStream, "Close"); Dispatch.putRef(spVoice, "AudioOutputStream", null); spAudioFormat.safeRelease(); spFileStream.safeRelease();*/ spVoice.safeRelease(); ax.safeRelease(); } catch (Exception e) { e.printStackTrace(); } } }
-
從測試類可以看出,這個方法既可以發聲還能輸出后綴為
.wav
的文件,這是一個標准的多媒體文件。上述代碼注釋很清晰,就不解釋了,自己看哈。 -
測試成功,現在集成到自己的項目中。
另述
這里說到了調用揚聲器發聲,不放還可以想一下如何調用麥克風收音。
public class EngineeCore {
String filePath = "E:\\voice\\voice_cache.wav";
AudioFormat audioFormat;
TargetDataLine targetDataLine;
boolean flag = true;
private void stopRecognize() {
flag = false;
targetDataLine.stop();
targetDataLine.close();
}
private AudioFormat getAudioFormat() {
float sampleRate = 16000;
// 8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
// 8,16
int channels = 1;
// 1,2
boolean signed = true;
// true,false
boolean bigEndian = false;
// true,false
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}// end getAudioFormat
private void startRecognize() {
try {
// 獲得指定的音頻格式
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
// Create a thread to capture the microphone
// data into an audio file and start the
// thread running. It will run until the
// Stop button is clicked. This method
// will return after starting the thread.
flag = true;
new CaptureThread().start();
} catch (Exception e) {
e.printStackTrace();
} // end catch
}// end captureAudio method
class CaptureThread extends Thread {
public void run() {
AudioFileFormat.Type fileType = null;
File audioFile = new File(filePath);
fileType = AudioFileFormat.Type.WAVE;
//聲音錄入的權值
int weight = 2;
//判斷是否停止的計數
int downSum = 0;
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
AudioInputStream ais = null;
try {
targetDataLine.open(audioFormat);
targetDataLine.start();
byte[] fragment = new byte[1024];
ais = new AudioInputStream(targetDataLine);
while (flag) {
targetDataLine.read(fragment, 0, fragment.length);
//當數組末位大於weight時開始存儲字節(有聲音傳入),一旦開始不再需要判斷末位
if (Math.abs(fragment[fragment.length-1]) > weight || baos.size() > 0) {
baos.write(fragment);
System.out.println("守衛:"+fragment[0]+",末尾:"+fragment[fragment.length-1]+",lenght"+fragment.length);
//判斷語音是否停止
if(Math.abs(fragment[fragment.length-1])<=weight){
downSum++;
}else{
System.out.println("重置奇數");
downSum=0;
}
//計數超過20說明此段時間沒有聲音傳入(值也可更改)
if(downSum>20){
System.out.println("停止錄入");
break;
}
}
}
//取得錄音輸入流
audioFormat = getAudioFormat();
byte audioData[] = baos.toByteArray();
bais = new ByteArrayInputStream(audioData);
ais = new AudioInputStream(bais, audioFormat, audioData.length / audioFormat.getFrameSize());
//定義最終保存的文件名
System.out.println("開始生成語音文件");
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, audioFile);
downSum = 0;
stopRecognize();
} catch (Exception e) {
e.printStackTrace();
} finally {
//關閉流
try {
ais.close();
bais.close();
baos.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
}// end run
}// end inner class CaptureThread
這個測試沒測試,偷個懶找的“哈哈”。
還有一點是Java操作語音文件.wav
先不要研究了 :laugh and cry:,這里涉及到了語音識別,但是有百度那么些api,有興趣的試試吧!
好了,在這里就結束了
更新
博主把自己的畢設項目打包放到自己的服務器上,這個揚聲器出現了新的問題。
本來所有的基礎都是在本地運行的,通過調用本地dll文件實現揚聲器發聲,現在部署到centOS上將會失去這個dll的支持,目前所存在的問題是如何不使用dll文件實現這個功能,中間借助了
.wav
后綴的音視頻文件。
- 如何在Linux上生成
.wav
的文件。- 如何獲取這個文件並輸出。(解釋一下,用餐顧客點擊手機網頁的菜單,然后再餐廳的主機來播放這個聲音)
- 如何在輸出主機不進行任何操作就能播放這個聲音或者能夠恢復之前的工作狀態。
現在的臨時解決辦法是本地跑一個呼叫服務的接口,當需要這個功能的時候遠程服務器調用本地跑的接口,進而實現餐廳主機發聲。
這個和上面描述的並無差別,不一樣的是存在了兩台主機的調用(當然兩台主機都應該鏈接網絡,能夠互相通信)
先寫到這了,當有解決辦法的時候再更新吧!