- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- java 調用本地揚聲器的步驟
接下來(lái)就對這個(gè)小功能進(jìn)行分析和實(shí)現。先寫(xiě)一個(gè)Demo。
1.首先,我們需要一個(gè)dll作為輔助。這里解釋一下dll的含義(DLL(Dynamic Link Library)文件為動(dòng)態(tài)鏈接庫文件,又稱(chēng)“應用百程序拓展”,是軟件文件類(lèi)型。在Windows中,許多應用程序并不是一個(gè)度完整的可執行文件,它們被分割成一些相知對獨立的動(dòng)態(tài)鏈接庫,即DLL文件,放置于道系統中。當我們執行某一個(gè)程序時(shí),相應的版DLL文件就會(huì )被調用。一個(gè)應用程序可使用權多個(gè)DLL文件,一個(gè)DLL文件也可能被不同的應用程序使用,這樣的DLL文件被稱(chēng)為共享DLL文件)。
需要把jacob-1.17-M2-x64.dll復制到C:\Windows\System32\目錄下。我們也能看到目錄下有很多的.dll文件。
這里的文件大家自己百度下,很好找的。
2.使用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>
3.測試類(lèi)代碼。
/** * 文字轉語(yǔ)音測試 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顧客正在尋求幫助??!"); } /** * 語(yǔ)音轉文字并播放 * * @param text */ public static void textToSpeech(String text) { ActiveXComponent ax = null; try { ax = new ActiveXComponent("Sapi.SpVoice"); // 運行時(shí)輸出語(yǔ)音內容 Dispatch spVoice = ax.getObject(); // 音量 0-100 ax.setProperty("Volume", new Variant(100)); // 語(yǔ)音朗讀速度 -10 到 +10 ax.setProperty("Rate", new Variant(0)); // 執行朗讀 Dispatch.call(spVoice, "Speak", new Variant(text)); /* // 下面是構建文件流把生成語(yǔ)音文件 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); // 調用輸出 文件流打開(kāi)方法,創(chuàng )建一個(gè).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)); // 開(kāi)始朗讀 Dispatch.call(spVoice, "Speak", new Variant(text)); // 關(guān)閉輸出文件 Dispatch.call(spFileStream, "Close"); Dispatch.putRef(spVoice, "AudioOutputStream", null); spAudioFormat.safeRelease(); spFileStream.safeRelease();*/ spVoice.safeRelease(); ax.safeRelease(); } catch (Exception e) { e.printStackTrace(); } } }
4.從測試類(lèi)可以看出,這個(gè)方法既可以發(fā)聲還能輸出后綴為.wav的文件,這是一個(gè)標準的多媒體文件。上述代碼注釋很清晰,就不解釋了,自己看哈。
5.測試成功,現在集成到自己的項目中。
這里說(shuō)到了調用揚聲器發(fā)聲,不放還可以想一下如何調用麥克風(fēng)收音。
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時(shí)開(kāi)始存儲字節(有聲音傳入),一旦開(kāi)始不再需要判斷末位 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); //判斷語(yǔ)音是否停止 if(Math.abs(fragment[fragment.length-1])<=weight){ downSum++; }else{ System.out.println("重置奇數"); downSum=0; } //計數超過(guò)20說(shuō)明此段時(shí)間沒(méi)有聲音傳入(值也可更改) 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("開(kāi)始生成語(yǔ)音文件"); AudioSystem.write(ais, AudioFileFormat.Type.WAVE, audioFile); downSum = 0; stopRecognize(); } catch (Exception e) { e.printStackTrace(); } finally { //關(guān)閉流 try { ais.close(); bais.close(); baos.reset(); } catch (IOException e) { e.printStackTrace(); } } }// end run }// end inner class CaptureThread
這個(gè)測試沒(méi)測試,偷個(gè)懶找的“哈哈”。
還有一點(diǎn)是Java操作語(yǔ)音文件.wav先不要研究了 :laugh and cry:,這里涉及到了語(yǔ)音識別,但是有百度那么些api,有興趣的試試吧!
好了,在這里就結束了
博主把自己的畢設項目打包放到自己的服務(wù)器上,這個(gè)揚聲器出現了新的問(wèn)題。
本來(lái)所有的基礎都是在本地運行的,通過(guò)調用本地dll文件實(shí)現揚聲器發(fā)聲,現在部署到centOS上將會(huì )失去這個(gè)dll的支持,目前所存在的問(wèn)題是如何不使用dll文件實(shí)現這個(gè)功能,中間借助了.wav后綴的音視頻文件。
如何在Linux上生成.wav的文件。
如何獲取這個(gè)文件并輸出。(解釋一下,用餐顧客點(diǎn)擊手機網(wǎng)頁(yè)的菜單,然后再餐廳的主機來(lái)播放這個(gè)聲音)
如何在輸出主機不進(jìn)行任何操作就能播放這個(gè)聲音或者能夠恢復之前的工作狀態(tài)。
現在的臨時(shí)解決辦法是本地跑一個(gè)呼叫服務(wù)的接口,當需要這個(gè)功能的時(shí)候遠程服務(wù)器調用本地跑的接口,進(jìn)而實(shí)現餐廳主機發(fā)聲。
這個(gè)和上面描述的并無(wú)差別,不一樣的是存在了兩臺主機的調用(當然兩臺主機都應該鏈接網(wǎng)絡(luò ),能夠互相通信)
先寫(xiě)到這了,當有解決辦法的時(shí)候再更新吧!
以上就是java 調用本地揚聲器的步驟的詳細內容,更多關(guān)于java 調用本地揚聲器的資料請關(guān)注腳本之家其它相關(guān)文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站